I don’t think you quite understand how reduce works, and you’re not returning a value anywhere in your functions.
So to walk through the code:
arr is (to use the example) [7, "ate", "", false, 9]
First elem is the value 7.
nwarr is passed into reduce and is [false, null,0,"",undefined,NaN] (although in your code it’s irrelevant what the values are as long as it’s an array or an object).
You loop six times, replacing the relevant value in nwarr with the type of the element.
nwarr is now `[“number”, “number”, “number”, “number”, “number”]
You don’t return anything from the function, so the return value is undefined
Second elem is the string "ate"
return value last time the function was called was undefined, so
nwarr now has the value undefined
You attempt to loop six times, replacing the relevant value in nwarr with the type of the element.
undefined doesn’t have anything at index 0, 1, 2, 3, 4 or 5. In fact it has no indices because it’s undefined, not an array.
function errors, exit.
If I actually fix this to return a value (the callback for reducemust return the accumulator nwarr each time), which is what I think you meant to do:
function bouncer(arr) {
return arr.reduce((nwarr,elem)=>{
for(let i = 0 ; i < 6; i++){
console.log(nwarr[i] = typeof elem)
}
return nwarr;
},[false, null,0,"",undefined,NaN])
}
This happens:
first element is 7. 7 has the type number. nwarr is now ["number", "number", "number", "number", "number", "number"]
second element is “ate”. “ate” has the type string. nwarr is now ["string", "string", "string", "string", "string", "string" ]
third element is “”. “” has the type string. nwarr is now ["string", "string", "string", "string", "string", "string" ]
fourth element is false. false has the type boolean. nwarr is now ["boolean", "boolean", "boolean", "boolean", "boolean", "boolean"]
fifth element is 9. 9 has the type number. nwarr is now ["number", "number", "number", "number", "number", "number"]
Finished, no more elements, return value is ["number", "number", "number", "number", "number", "number"]
Above is what happens, but can you try to explain what you thought would happen here?