数组操作】Fix the Bug: Simple Array Manipulation"/>
0058【Edabit ★☆☆☆☆☆】【改bug:数组操作】Fix the Bug: Simple Array Manipulation
0058【Edabit ★☆☆☆☆☆】【改bug:数组操作】Fix the Bug: Simple Array Manipulation
arrays
bugs
language_fundamentals
Instructions
Help fix all the bugs in the function
incrementItems
! It is intended to add 1 to every element in the array!
Examples
incrementItems([0, 1, 2, 3]) // [1, 2, 3, 4]
incrementItems([2, 4, 6, 8]) // [3, 5, 7, 9]
incrementItems([-1, -2, -3, -4]) // [0, -1, -2, -3]
Notes
- Make sure to read every line carefully.
Solutions
// bugs
function incrementItems(arr) {for (let i = 0; i < array.length; i++) // typo `array`arr[i] === arr[i] + 1 // `===` -> `=`return array // typo `array`
}
// correct it !!
function incrementItems(arr) {for (let i = 0; i < arr.length; i++) //arr[i] = arr[i] + 1 //return arr //
}
TestCases
let Test = (function(){return {assertEquals:function(actual,expected){if(actual !== expected){let errorMsg = `actual is ${actual},${expected} is expected`;throw new Error(errorMsg);}},assertSimilar:function(actual,expected){if(actual.length != expected.length){throw new Error(`length is not equals, ${actual},${expected}`);}for(let a of actual){if(!expected.includes(a)){throw new Error(`missing ${a}`);}}}}
})();Test.assertSimilar(incrementItems([0, 1, 2, 3]), [1, 2, 3, 4])
Test.assertSimilar(incrementItems([2, 4, 6, 8]), [3, 5, 7, 9])
Test.assertSimilar(incrementItems([-1, -2, -3, -4]), [0, -1, -2, -3])
更多推荐
0058【Edabit ★☆☆☆☆☆】【改bug:数组操作】Fix the Bug: Simple Array Manipulation
发布评论