array manipulation in javascript code example
Example 1: modify array elements javascript
Using a FOR loop, write a function addNumber which adds the argument n to each
number in the array arr and returns the updated arr:
const addNumber=(arr, n)=>{
let arr1=[];
for(let i=0;i<Math.max(arr.length);i++){
arr1.push((arr[i]||0)+n)
}
return arr1;
}
console.log(addNumber([4, 5, 6], 7));
Example 2: functions in arrays javascript
var argsContainer = ['hello', 'you', 'there'];
var functionsContainer = [];
for (var i = 0; i < argsContainer.length; i++) {
var currentArg = argsContainer[i];
functionsContainer.push(function(currentArg){
console.log(currentArg);
});
};
for (var i = 0; i < functionsContainer.length; i++) {
functionsContainer[i](argsContainer[i]);
}