Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to understand some basic Javascript logic

const nums = [1, 50, 75, 200, 350, 525, 1000];
const smallerNums = nums.map(num => num - 5);
console.log(smallerNums);

Hello, Javascript newb here. The above is taken from CodeAcademy. I just want to make sure I understand the basic logic here:

Is this saying the new array ‘smallerNums’ is built when ‘num – 5’ is returned. The parameter num, is what is ‘iterating’ through the established array nums, to identify each element, then for ‘num – 5’ to take this value, subtract five and hence find a new value which will appear in the smallerNums array.

Terminal, btw :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
   -4,  45,  70, 195,
  345, 520, 995
]

>Solution :

Yes you are basically mapping the value of nums array subtracted by 5 to a new array which is smallerNums.

map() creates a new array from calling a function for every array element.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading