I want to test a simple program with node:
let myArray = [1, 2, 3, 4, 5];
function doubleInput(arrayItem) {
return arrayItem * 2;
}
But for some reason nothing outputs when i do node.js in the terminal. Am i missing something? I do have node installed (v20.3.0).
My output should be:
2, 4, 6, 8, 10
But sadly, nothing shows up
I simply have just a test.js file that has an array with values and then a function that multiplies by two and returns that result. I tried adding a .npmignore file, a package lock and package.json adding in i still have the same results. Also in terminal i do
node test.js
3
Answers
Your code outputs nothing, because you’re not calling your function, and you’re not printing anything.
I would also suggest using
map
in your case. You can use it like this:Outputs
You actually didn’t call your function
doubleInput
, that’s why you didn’t get any output,Even though you invoke it, you gonna get
NaN
sincereturn arrayItem * 2
; is like[1, 2, 3, 4, 5] * 2
which always givesNaN
;The right way is looping over each value, Doubling it and storing it in an array, and finally returning that variable.
You can use
map
method andjoin