私はので、私は本当に誰かが私はcoderfightのコーダの挑戦を通じてつもりで何が起こっているか/なぜ例外RangeError:1つの環境に無効な配列の長さではなく、別の
私に言っていただければ幸いJavaScriptに比較的新しいです。私はJSのフィドルで動作するコードを持っていますが、エラーでcodefightsに失敗:
は"RangeError: Invalid array length
at matrixElementsSum (file.js on line 25:17)
at _runyckrx (file.js on line ?:28)
at getUserOutputs (file.js on line ?:30)
at Object.<anonymous> (file.js on line ?:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:148:18)"
問題 - 配列を積み重ね、そしてあなたが特定のインデックスでゼロを見つけた場合は、以下の各配列は、同じインデックスを持っています値がゼロである
例:
[[0, 1, 1, 2],
[0, 5, 0, 0],
[2, 0, 3, 3]]
は次のようになります
[[x, 1, 1, 2],
[x, 5, x, x],
[x, x, x, x]]
その後、問題が最も可能性が高いここで、最終的な配列
matrix = [[0,1,1,2],
[0,5,0,0],
[2,0,3,3]]
function matrixElementsSum(matrix) {
// Calculate the width and height of the Array
var w = matrix.length || 0;
var h = matrix[0] instanceof Array ? matrix[0].length : 0;
// In case it is a zero matrix return empty array.
if(h === 0 || w === 0) { return []; }
var i, j, t = [];
// Loop through every item in the outer array (height)
for(i=0; i<h; i++) {
// Insert a new row (array)
t[i] = [];
// Loop through every item per item in outer array (width)
for(j=0; j<w; j++) {
// Save transposed martix.
t[i][j] = matrix[j][i];
}
}
// rows are now cols - if we come across a 0 the remaining values
//dont need to be there
for(var i = 0; i < t.length; i++){
t[i].length = t[i].indexOf(0);
}
//concat multi array to single array
var merged = [].concat.apply([], t);
// get sum of flattened array
var totalCost = merged.reduce(function(acc, val) {
return acc + val;
}, 0);
return totalCost
}
matrixElementsSum(matrix)
どのラインでエラーが発生していますか? – Barmar