Question: JavaScript write code in this function to iterate through the array and to add 1 to each numeric element of the array (which is passed
JavaScript
write code in this function to iterate through the array and to add 1 to each numeric element of the array (which is passed in as a parameter).
NOTE: In the first test: The array contains only numeric elements. However, in the second test: The array contains some numeric elements, and some non-numeric elements.
a for loop and the typeof operator should try to be used.
* @param {*[]} arrToProcess array to be processed
* @return {*[]} array after processing, with the value of all numerical elements being 1 greater than in the original array.
function forLoop(arrToProcess) {
//code should be in here and should return arrToProcess
return arrToProcess;
}
this is the test it should pass;

// test the for loop function. QUnit.test('Test for loop function', function( assert ) { let arrTest1 = [1,2,3,4,5]; let arrResult1 = [2,3,4,5,6); let arrTest2 = [1,2,3,4,"hello", "5",6]; let arrResult2 = [2,3,4,5,"hello", "5",7]; assert.equal(forLoop(arrTest1).toString(), arrResult1.toString(), "for loop testi worked"); assert.equal(forLoop(arrTest2).toString(), arrResult2.toString(), "for loop test2 worked (includes non numeric elements)"); })
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
