0058【Edabit ★☆☆☆☆☆】【改bug:数组操作】Fix the Bug: Simple Array Manipulation

编程入门 行业动态 更新时间:2024-10-25 13:30:53

0058【Edabit ★☆☆☆☆☆】【改bug:<a href=https://www.elefans.com/category/jswz/34/1771288.html style=数组操作】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

本文发布于:2023-11-16 18:23:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629999.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   操作   Edabit   bug   Manipulation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!