2015-08-25 73 views
3

我是JavaScript新手,我正在努力解決這個課堂上的一個問題。這可能很簡單,但我現在完全陷入困境。如何替換數組中的值?

無論如何,現在的問題是:

我要創建基於行和列的用戶指定數目的xo交替字符表。舉例來說,如果用戶想要的3行3列,那就要看起來像這樣:

xox 
oxo 
xox 

我完全失去了對如何創建數組中交替值。這是我迄今爲止(下文)的內容,但是這是完全錯誤的。如果有人能給我一些很棒的建議!我一直在看這個問題好幾天,但似乎無法將它拼湊在一起。

// a = user input # of columns 
// b = user input # of rows 

function firstTest(a,b) { 
    var firstArray = []; 
    var total = []; 
    for (i = 0; i < a; i+=1) {   
    firstArray.push("xo"); 
    } 
    for (i=0; i<b; i+=1){ 
    total.push(firstArray); 
    } 
    return(total); 
} 
+0

http://www.andrespagella.com/getting-even-values-array-without-工作循環 –

+0

http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript – Xavjer

+0

如果你需要一個方格圖案,那麼到目前爲止答案是錯誤的。 – aross

回答

1

你只需要檢查,如果該行的值和列值之和爲奇數或偶數:

function firstTest(a,b) { 
    table = []; 
    for (x = 1 ; x <= a ; x++) { 
     row = []; 
     for (y = 1 ; y <= b ; y++) { 
      if (((x+y) % 2) == 0) { 
       row.push('x'); 
      } else { 
       row.push('o'); 
      } 
     } 
     table.push(row); 
    } 
    return table; 
} 
+0

這是唯一答案總是產生方格圖案。 – aross

+0

感謝您的幫助!我相信下面看到的所有答案都非常相似。我知道,知道模運算符,你可以檢查是否有偶數或奇數。此外,我想我只是混淆了自己如何定位陣列,推動兩個不同的用戶輸入。 – Kevin

1

您可以與布爾變量替代值。例如:

var _switch = false; 
// your code here... 
if (_switch) firstArray.push("o"); 
else firstArray.push("x"); 
// more core here... 
_switch = !_switch; 

您的代碼:

// a = user input # of columns 
// b = user input # of rows 

function firstTest(a,b) { 
    var _switch = false; 
    var firstArray = []; 
    var total = []; 
    for (i = 0; i < a; i++) {   
    if (_switch) firstArray.push("x"); 
    else firstArray.push("o"); 
    _switch = !_switch; 
    } 
    for (i=0; i<b; i+=1){ 
    total.push(firstArray); 
    } 
    return(total); 
} 
+0

有趣......我知道有一些東西具有我可以使用的布爾值。我想我只是不知道如何使用它。仍在學習!但學到了新東西。謝謝 – Kevin

+0

Np。隨時upvote我的答案,如果它確實幫助你=)。 – PinkTurtle

0

我不知道這是你想達到什麼,但我想你想要的值從細胞到細胞交替:

function createAlternatingTable (rows, cols) { 
    var table = []; 
    var cellCount = 0; 
    for (var rowIndex = 0; rowIndex < rows; rowIndex++) { 
     var row = []; 
     for (var colIndex = 0; colIndex < cols; colIndex++) { 
      row.push(cellCount % 2 == 0 ? 'x' : 'o'); 
      cellCount++; 
     } 
     table.push(row); 
    } 
    return table; 
} 

訣竅是cellCount % 2 == 0。所以當插入單元數爲'x'時,如果是奇數,則插入'o'

0

你需要的是一個2維數組,其中外部數組將具有行,而內部數組將具有每行中的列。

一個簡單的邏輯,我們可以遵循的是,我們將創建第2排那麼我們可以克隆他們創造他們的休息

function firstTest(a, b) { 
 

 
    var total = [], 
 
    tmp; 
 
    for (var i = 0; i < a; i++) { 
 
    if (i < 2) { 
 
     tmp = []; 
 
     for (var j = 0; j < b; j++) { 
 
     tmp.push((i * b + j) % 2 == 0 ? 'x' : 'o'); 
 
     } 
 
    } else { 
 
     tmp = total[i % 2].slice(); 
 
    } 
 
    total.push(tmp) 
 
    } 
 
    return total; 
 
} 
 

 
snippet.log(JSON.stringify(firstTest(1, 1))); 
 
snippet.log(JSON.stringify(firstTest(2, 1))); 
 
snippet.log(JSON.stringify(firstTest(2, 3))); 
 
snippet.log(JSON.stringify(firstTest(3, 2))); 
 
snippet.log(JSON.stringify(firstTest(3, 3)));
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> 
 
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

0

可以使用模(%)至交替數組中的不同字符。 模數選項查找除數除以除數時的餘數。 (14%5 =(0,8 * 5)= 4)。

我寫的這個例子將交替使用「A」,「B」和「C」(可以擴展爲「what ever」)。 要使其交替出現「X」和「O」,只需定義var altChars = ["X","O"];

function buildTable(a,b) 
{ 
    var returnTable = []; 
    var altChars = ["A","B","C"]; 
    for (i = 0; i < a; i++) 
    { 
     returnTable[i] = []; 
     for (j = 0; j < b; j++) 
     { 
      returnTable[i][j] = altChars[ ((i * a) + j + i) % altChars.length]; 
     } 
    } 
return returnTable; 
} 

console.log(buildTable(4,5)); 

專爲示範開始代碼的的jsfiddle:http://jsfiddle.net/kdgwbr2q/

0

試試這個

function martix (row,col) { 

    var total_sym= row*col; 
    var strSym = ""; 
    for(var i =1;i<=total_sym;i++){ 

     if(i%2){ 
      strSym+='X'; 
     } else{ 
      strSym+='O'; 
     } 
    } 

    return strSym; 
} 


    var strSym = martix(3,3); 
    var arrSym = strSym.split(''); //["X", "O", "X", "O", "X", "O", "X", "O", "X"] 
1

解決方案與Array.applyArray.prototype.map

var width = 3, 
 
    height = 3, 
 
    pattern = 'xo', 
 
    array = Array.apply(Array, { length: height }).map(function (_, i) { 
 
     return Array.apply(Array, { length: width }).map(function (__, j) { 
 
      return pattern[(i + j) % 2]; 
 
     }); 
 
    }); 
 

 
document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');

的壓縮碼:

var array = Array.apply(Array, { length: 3}).map(function (_, i) { 
 
     return Array.apply(Array, { length: 3}).map(function (__, j) { 
 
      return 'xo'[(i + j) % 2]; 
 
     }); 
 
    }); 
 

 
document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');

+1

哇,我喜歡這個。簡短而甜美。 +1 – DavidDomain

0

有趣的挑戰。

function xo(a,b) { 

    var table = [], i, j; 

    for (i = 0; i < a; i++) { 
    var row = []; 
    for (j = 0; j < b; j++) { 
     row.push(((j+i) % 2) ? 'o' : 'x'); 
    } 
    table.push(row); 
    } 
    return table; 
} 

xo(3,4)將返回:

| x,o,x,o |
| o,x,o,x |
| x,o,x,o |

0

播放與Plunker

精彩讓這樣的事情:-)

var getArray = function(rows, columns) { 

    var curCell = 0; 
    var table = []; 

    for (var iRow = 0; iRow < rows; iRow++) { 
     var row = []; 
     for (var iCol = 0; iCol < columns; iCol++) { 
      row.push(curCell % 2 == 0 ? 'x' : 'o'); 
      curCell++; 
     } 
     table.push(row); 
    } 

    for (var i=0; i<table.length; i++) { 
    console.log(table[i]); 
    } 
}