const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
"use strict";
// change code below this line
const squaredIntegers = arr.map(num => (num=>0 && num%2===0)?num*num:[]);
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>
) will also add backticks around text.
Note: Backticks are not single quotes.
No you can’t , map always returns the exact number of elements of your input array , so if you want to be selective on the output array, have to use filter or , a for loop or forEach , like:
let output=[]
arr.forEach(num => {
if (num>=0 && num%2===0) output.push(num*num)
})
return output
FYI, you have a syntax error/typo num=>0
should be num>=0
otherwise your map will return functions for elements in the output array since =>
is reserved
1 Like