一道有意思的JS面试题目,求答案
题目是这样的:
<- 1234567 ->
<- 2345671 ->
<- N-2 N-1 N 1 2 ->
左右箭头每点击一次,向左或向右移动一格,当到达最大值N时,循环显示(数字头尾相连);
小弟的分实在是少哇, 还望各位能不吝赐教啊!
给说下思路,最好能附上源码!多谢~
我自己写了个,但是只实现部分的这种效果
<script>
list = ['1','2','3','4','5','6','7'];
var start=-1;
function go(){
start+=1;
var i=start;
var node=document.getElementById('show').firstChild;
node.appendData('\n');
for(var j=0;j<5;j++){
node.appendData(list[i%7]);
i++;
}
}
</script>
<button onclick='go()'><</button>
<div id='show'>here</div>
<button onclick='go()'>></button>
------解决方案--------------------<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function be(){
var f=document.getElementById("test")
var con=f.innerHTML;
con=con.substring(1)+con.substring(0,1);
f.innerHTML=con;
}
function en(){
var f=document.getElementById("test")
var con=f.innerHTML;
con=con.substring(con.length-1)+con.substring(0,con.length-1);
f.innerHTML=con;
}
</script>
</head>
<body>
<font color=red onclick="be()"><-</font><font id="test">1234567</font><font color=red onclick="en()">-></font>
</body>
</html>
不知是不是你想要的
------解决方案--------------------
写的很烂
<html>
<head>
<script>
var list = ['1','2','3','4','5','6','7'];
var tempList = [];
var joinFlag =",";
var current=0;
var totalNum = 7;
function pushArray(array , value)
{
array[current] = value;
}
function popArray(array)
{
var index = getCurrent();
var temp = array[index];
array[index] = null;
return temp;
}
function getCurrent()
{
return current;
}
function addCurrent()
{
current =(current+1)%totalNum;
}
function go()
{
var arrayNow = 0;
if(tempList[current] && tempList[totalNum-1])
{
var value = popArray(tempList);
pushArray(list, value);
}
else
{
var value = popArray(list);
pushArray(tempList, value);
arrayNow = 1;
}
addCurrent();
var node=document.getElementById('show').firstChild;