function calc(n,func){
let foo = [];
for (let i = 1; i <=n; i++) {
foo.push(i);
}
foo=foo.map(func)
return foo
}
function helloworld(x){
let ans=calc(5,calcmystery)
return ans
}
calcmystery=(n,x)=>(n+x*n)/(2)
I have three functions here, first is calc function, helloworld, calcmystery.
My calcmystery takes in two arguments and return a value based on the mathematical operation above.
My calc function takes in two arguments, the n parameter is to create an array of values 1-N and the func parameter to map and transform the values in each array according to the function passed into.
My helloworld function takes in one argument x, calls the calc function and return it.
My problem here is, since I only have one argument x, when i call calc inside the helloworld function, the, and when the calcmystery function inside the calc function is called, the x parameter passed into calcmystery is not the x value I passed into the hello world function. How do I fix this code, so the x value passed into is the same and always the same value passed into the helloworld function?
console.log(helloworld(2))
It should output
[1.5,3,4.5,6,7.5]
But the output is
[0.5,2,4.5,8,12.5]