I'm new to js and have this task that confuses the hell out of me.
The task is to "use Array.map() to create a new array that contains the last digit of all prime numers". I already have an array 'primes' containing the prime numbers.
It actually works fine if I only want to print the last digit for each item of the primes array using console.log:
let lastDig = new Array()
lastDig = primes.map((value, index) => console.log(value % 10))
// -> 2,3,5,7,1,3,7,...
However, if I try to then add the digit to the array I'm supposed to fill in the task, it just ends up filling it with 1,2,3,4,5,..., 25, even though the ONLY thing I changed in the code was to replace console.log(value % 10) by lastDig.push(value % 10).
let lastDig = new Array()
lastDig = primes.map((value, index) => lastDig.push(value % 10))
// -> lastDig contains: 1,2,3,4,5,..., 25
How can I make this work and why does it change the values if I barely changed the code?
.map()returns a new array and it's generally expected that its return value is the intended result of the operation. Relying on.push()in this way instead should probably be done with a normal loop, not with.map().