In: Computer Science
JavaScript
Write out a function that takes in array of numbers. You do not need to check if each item of the array is a number, assume this has already been done. Create a new array using the map function that that the original item and adds 4 to it. Then iterate through the new array and log each item to the console. If you do not use map it will be counted wrong.
Call your function with the following array of data and write a comment with the outputted result (e.g. do the math).
[4, -4, 10, 203, 53, 11]
Here is the answer for your question in JavaScript Scripting Language.
Kindly upvote if you find the answer helpful.
###################################################################
CODE :
function mapArray(x){ //Use map function and add 4 to each original item x = x.map(addFour); //Function to add 4 to each item of array function addFour(num){ return num + 4; } //Display output with comment console.log("Do the math: "); for(var i = 0;i<x.length;i++) console.log(x[i] + " "); } mapArray([4, -4, 10, 203, 53, 11]); |
#################################################################
SCREENSHOTS :
Please see the screenshots of the code below for the indentations of the code.
#######################################################
OUTPUT :
Any doubts regarding this can be exlained with pleasure :)