so I'm solving this task where I need to return the given array after casting all numbers in it to Boolean and all strings to numbers.
My current code looks like this:
const arr = [0, "2", 3, 4, "s"];
function castArr(arr) {
return arr.map(function(elem) {
if (typeof elem === Number) {
return !!elem;
} else {
return Number(elem)
}
})
}
I have tried also using
if (typeof elem === Number) {
return Boolean(elem)
but it still won't convert - in the result I have the same numbers (0, 3, 4).
Can you help me find the mistake here? I'm sure it's not hard, but I am kinda lost...
CodePudding user response:
Do changes as follows then it will work
const arr = [0, "2", 3, 4, "s"];
function castArr(arr) {
return arr.map(function(elem) {
if (typeof elem === 'number') {
return Boolean(elem);
} else {
return Number(elem)
}
})
}
console.log(castArr(arr))