# Basic Algorithm Scripting: Falsy Bouncer ?not working

**Tell us what’s happening:
can anyone edit my code to be correct?

``````
function bouncer(arr) {
arr.reduce((nwarr,elem)=>{
for(let i = 0 ; i < 6; i++){
console.log(nwarr[i] = typeof elem)
}

},[false, null,0,"",undefined,NaN])
}

bouncer([7, "ate", "", false, 9]);
/*
for(let i = 0 ; i < arr.length; i++){
console.log(typeof elem===falsy[i])
}
*/
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36`.

Challenge: Falsy Bouncer

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]`

1. First `elem` is the value `7`.
2. `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).
3. You loop six times, replacing the relevant value in `nwarr` with the type of the element.
4. `nwarr` is now `[“number”, “number”, “number”, “number”, “number”]
5. You don’t return anything from the function, so the return value is `undefined`

1. Second `elem` is the string `"ate"`
2. return value last time the function was called was `undefined`, so
3. `nwarr` now has the value `undefined`
4. You attempt to loop six times, replacing the relevant value in `nwarr` with the type of the element.
5. `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.
6. function errors, exit.

If I actually fix this to return a value (the callback for `reduce` must 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:

1. first element is 7. 7 has the type `number`. `nwarr` is now `["number", "number", "number", "number", "number", "number"]`
2. second element is “ate”. “ate” has the type `string`. `nwarr` is now `["string", "string", "string", "string", "string", "string" ]`
3. third element is “”. “” has the type `string`. `nwarr` is now `["string", "string", "string", "string", "string", "string" ]`
4. fourth element is false. false has the type `boolean`. `nwarr` is now `["boolean", "boolean", "boolean", "boolean", "boolean", "boolean"]`
5. fifth element is 9. 9 has the type `number`. `nwarr` is now `["number", "number", "number", "number", "number", "number"]`
6. 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?

2 Likes