2015-09-06 22 views
1

誰能告訴我什麼是錯,此代碼得到N個從數組從給定的點開始數

var list = [0, 1, 2, 3, 4, 5, 6, 7]; 
function get_next_num(point, direction, count){ 
    var results = []; 
    for(i = point; i < count; i++){ 
     if(direction == 'left'){ 
      i = -i; 
     } 

     if(i > list.length - 1) 
      i = 0; 

     if(i < 0) 
      i = list.length - 1;  

     results.push(i); 
    } 

    return results; 
} 

它應該返回從從給定的點開始的數組號的列表,但它僅適用如果我從0

開始如果我從6開始,例如我什麼也沒得到

警報(get_next_num(6, '右',3));

但它應該返回7,0,1

+0

對於初學者來說,如果方向是向左,你切換在每個迭代上的減號。實際上,你想要做的就是使用[Array.prototype.silce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice)。 – Nit

+0

你正在推'i'而不是'list [i]'。另外,你從'point'開始並繼續'count'。如果'point'爲'5'而'count'爲'4',則不會得到任何結果。 '或者爲'(i = 0; i aioobe

+0

你真的想從'get_next_num(6,'right',3)';'獲得'7,0,1'嗎?如果你以6作爲列表的索引,那麼你會得到'6',而不是'7'。 –

回答

0

您使用for循環錯誤。您應該通過0開始i,而不是point。如果以point開頭,比如在你的例子中,i=6,循環運行的條件是i<count,這意味着6<3這是錯誤的,所以循環從未開始。

它應該是這樣的:

function get_next_num(point, direction, count){ 
    var results = []; 
    j = 1; 
    index = point 
    if(direction == 'left'){ 
      j = -1; 
    } 
    for(i = 0; i < count; i++){ 
     index = index + j; 
     if(index > list.length - 1) 
      index = 0; 

     if(index < 0) 
      index = list.length - 1;  

     results.push(list[index]); 
    } 

    return results; 
} 
0
function get_ex_num(point,direction,count){ 
    var results=[]; 
    if(direction=='right'&&point<count)return'error'; 
    else if(count<point)return'error'; 

    for(i=point;i<count;((direction=='right')?i++:i--)){//might case error but hey ho you get the idea and worth a try :p 
     results.push(list[i]); 
    } 
    return results; 
} 

我沒有測試任何東西我會留給你;)

你的問題是,當你濫用i當方向離開時。這並沒有改變它只是改變了i的方向。方向由++和或--確定。

我已經做到了,所以當方向離開。計數必須低於要運行的點數。當方向正確時,計數必須高於點數。

您可以像這樣使用此功能。

norm=get_ex_num(0,'left',7); 
backwards=get_ex_num(7,'right',6); 
error=get_ex_num(4,'left',5); 
error2=get_ex_num(5,'right',4); 

如果我做了對我的錯走的路我一直在WordPress插件約7小時用冷今天DX

也是另一個原因,你的代碼不工作是i只是一個索引。這不是list的關鍵值。

如果我犯了一個錯誤,這可能是因爲list[i]

檢查了這一點,如果它不工作:)

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

+0

IMO你應該測試代碼。 – Vidul

2

該解決方案從list[point]開始:

var list = [0, 1, 2, 3, 4, 5, 6, 7]; 
 

 
function get_next_num(point, direction, count) { 
 
    var results = [], 
 
     dir = direction === 'left' ? -1 : 1,  // used as a factor for the index 
 
     i, index; 
 
    for (i = 0; i < count; i++) {    // simple count up, no hassle 
 
     index = (point + i * dir) % list.length; // calculate the index 
 
     if (index < 0) {       // and respect boundaries 
 
      index += list.length;    // add length to get a positive number 
 
     } 
 
     results.push(index);      // push the value 
 
    } 
 
    return results; 
 
} 
 
document.write('<pre>' + JSON.stringify(get_next_num(6, 'right', 3), 0, 4) + '</pre>'); 
 
document.write('<pre>' + JSON.stringify(get_next_num(2, 'left', 8), 0, 4) + '</pre>');

該解決方案開始

  • direction = 'right'list[point + 1]
  • direction = 'left'list[point - 1]

var list = [0, 1, 2, 3, 4, 5, 6, 7]; 
 

 
function get_next_num(point, direction, count) { 
 
    var results = [], 
 
     dir = direction === 'left' ? -1 : 1,  // used as a factor for the index 
 
     i, index; 
 
    for (i = 1; i <= count; i++) {    // simple count up, no hassle 
 
     index = (point + i * dir) % list.length; // calculate the index 
 
     if (index < 0) {       // and respect boundaries 
 
      index += list.length;    // add length to get a positive number 
 
     } 
 
     results.push(index);      // push the value 
 
    } 
 
    return results; 
 
} 
 
document.write('<pre>' + JSON.stringify(get_next_num(6, 'right', 3), 0, 4) + '</pre>'); 
 
document.write('<pre>' + JSON.stringify(get_next_num(2, 'left', 8), 0, 4) + '</pre>');

+0

完美答案。 – Vidul

相關問題