JSFiddle - React, Tailwind, and code Playground
by gorillawit
JavaScript
/* set i to a function that accepts a number as a value
and returns the sum of num + 1 */
let i = num => num + 1;
let j = i(2);
/* alterNum gets passed two values, an array (num) and a function (fn) */
let alterNum = (nums, fn) => {
/* create a new emplty array to hold the new values */
let newNums = [];
/* iterate over each item in the array and push the value of the result
of fn(num) which is addOne(num) */
for (let num of nums) {
newNums.push(fn(num));
}
/* return new array */
return newNums;
}
/* set addOne to a function that accepts a value (num) and returns the sum or num + 1 */
let addOne = num => num + 1;
/* call alterNum and pass in two parameters, an array and a function call */
alert(alterNum([1,2,3], addOne)); // 2,3,4