const squareList = arr => {
// Only change code below this line
// Use filter to find the positive integers (No negative numbers or decimals)
// Use reduce to mutiply the positive numbers by their own value (example: answer should be [25, 9])
// return the new array
return arr;
// Only change code above this line
};
const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
I wrote notes about what I think should be done, but I am struggling with how I should go about it. Can someone please help me understand without giving me the answer?
In my own words, I would say if a value in the array is less than 0…then it is not positive. And if a value doesn’t have a remainder of 0…then it is not an integer but instead a decimal. Saying it in my own words seems simple but writing it as code is a different story.
Ahhhh so I should use the map method to mutiply the remaining numbers in the array by their own value. Something like:
const squareList = arr => {
// Only change code below this line
// Step 1: Use filter to find the positive integers (No negative numbers or decimals)
// Step 2: Use map to mutiply the positive numbers by their own value (example: answer should be [25, 9])
return arr;
// Only change code above this line
};
const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
When I am confused I write down the steps I think need to happen as if I were explaining it to a kid. I have done that but I am stuck when it comes to actually applying methods I have learned in previous lessons. Can someone help me out without giving me the answer?
const squareList = arr => {
// Only change code below this line
arr.filter(num => num > 0 && num //(num must have a remainder of 0 which indicates it is not a decimal))
arr.map(num => num * num);
return arr;
// Only change code above this line
};
const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
This is what I have now but I know I am doing something wrong
Ohhhh okay, so instead I should keep it how I had it the first time and indicate that these changes are going into a brand new array. Like this? :
const squareList = arr => {
// Only change code below this line
[].filter(num => num > 0 && num //(num must have a remainder of 0 which indicates it is not a decimal))
[].map(num => num * num);
return arr;
// Only change code above this line
};
const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
I am having trouble remembering how I can indicate that the numbers must not be decimals. I believe the % sign is for remainders but I don’t know how to go about using it for the second half of the filter syntax
const squareList = arr => {
// Only change code below this line
arr = [].filter(num => num > 0 && num //(num must have a remainder of 0 which indicates it is not a decimal))
arr = [].map(num => num * num);
return arr;
// Only change code above this line
};
const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
const squareList = arr => {
// Only change code below this line
squaredIntegers = arr.filter(num => num > 0 && num //(num must have a remainder of 0 which indicates it is not a decimal))
squaredIntegers = arr.map(num => num * num);
return arr;
// Only change code above this line
};
const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
I tried doing this first to make it look just like the example, but this gives me an error and says TypeError: “squaredIntegers” is read-only
const squareList = arr => {
// Only change code below this line
squareList = arr.filter(num => num > 0 && num) //(num must have a remainder of 0 which indicates it is not a decimal))
squareList = arr.map(num => num * num);
return arr;
// Only change code above this line
};
const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
Yes you can declare variables using var, let, and const. People don’t really use var a lot because it causes a lot of problems because it can be easily changed. Using const keeps whatever you’re declaring constant. So in this case I can use let like:
let positiveIntegersSquared = arr.filter(num => num > 0 && num) //(num must have a remainder of 0 which indicates it is not a decimal))
Sorry for being so slow, I know it’s probably frustrating
Okay I think this is looking a lot better, I am just having trouble indicating that the number must be an integer and not a decimal. I’m sure there’s multiple ways to do this but I was trying to find a way to say if the number has a remainder of 0 because then the computer should know that number is an integer
const squareList = arr => {
// Only change code below this line
let positiveIntegersSquared = arr.filter(num => {
if (num > 0 && num) return num; //(num must have a remainder of 0 which indicates it is not a decimal))
}).map(num => { return num * num });
return positiveIntegersSquared;
// Only change code above this line
};
const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
But right now I just have num for the second half of that if statement