首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

一路有意思的JS面试题目,求答案

2013-01-11 
一道有意思的JS面试题目,求答案题目是这样的:- 1234567 -- 2345671 -- N-2 N-1 N 1 2 -左右箭头每点

一道有意思的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> 
写的很烂


<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;     
node.appendData('\n');

for(var  counter=current;counter<totalNum;counter++)



if(arrayNow)
{
node.appendData(list[counter]); 
}
else
{
node.appendData(tempList[counter]); 
}   

for(var  counter=0;counter<current;counter++)

if(arrayNow)
{
node.appendData(tempList[counter]);
}
else
{
node.appendData(list[counter]); 
}   
}

}
</script>
</head>
<body>
<!--button onclick='go()'><</button-->
<div id='show'>here</div>
<button onclick='go()'>></button> 
</body>
</html>

热点排行