Tell us what’s happening:
The only part I’m missing is how to square it - I’m not sure how to call the array and square it so it’ll work. I would really like to gain an understanding so any and all help would be appreciated.
Your code so far
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
realNumberArray.filter((arr) => arr > 0);
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.
You assign a reference to arr into squaredIntegers, then you don’t actually do anything with it.
You should NOT reference realNumberArray in your function at all. Your function CAN NOT assume the presence of a global variable, or it will break if that variable doesn’t exist. Instead, notice that you are passed that array as a parameter, arr – use THAT.
You filter for positive numbers, yes – but not for integers.
You don’t (yet) square them.
In the last line of your function, you return squaredIntegers, which again you have left untouched. You basically assigned a reference to the original array, then sent that original array BACK to the calling function.
Instead, your line
const squaredIntegers = arr
might look better
const squaredArr = arr.filter( (element) => {/* do something to remove any element from arr that is neither positive NOR an integer */} )
.map( (element) => {/* do something to square the filtered list you just made */} );
return squaredArr;
Note that I’ve chained together a .filter() and a .map() – the filter function returns a shorter list, then the map function does something to each member of that shorter list and returns an array of that same length.
How do you filter for both? You’ve got the first part, the positive piece, but how do you make sure a given element is an integer? There are a couple of ways, I might look at parseInt() or isInteger().
Now, to your original question – how do a square a number? Given a variable number, it is squared by number * number. How might that apply here?