Need assistance in write-higher-order-arrow-functions

Challenge Name

write-higher-order-arrow-functions has an issue.

Issue Description

Hello. I found that my code does not work. Code (chain with filter and reduce methods) works well in browser console, but did not work in freeCodeCamp env.

Browser Information

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

Screenshot

Your Code

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
  "use strict";
  // change code below this line
  const squaredIntegers = arr;
  squaredIntegers.filter(val => val > 0 && val % 1 === 0).map(val => val * val);
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
2 Likes

According to MDN:

map does not mutate the array on which it is called (although callback, if invoked, may do so).

1 Like

Sorry for my stupidity. :expressionless: I forgot that filter and doesn’t modify array.

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
     "use strict";
     // change code below this line
    const squaredIntegers = arr.filter(x=>(x^0)===x).map(x=>x*x);
    // change code above this line
    return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
2 Likes

so hard to understand…

1 Like

Stelmakhin, I really like your answer, in particular this part:

arr.filter(x=>(x^0)===x)

I don’t fully grok what is happening, though, and I am questioning if it really fully works. It looks like you are XORing the number with 0, which in JS is a IEEE-754 64-bit floating-point number, and thus consists of a mantissa and an exponent. I found this link describing number representation in JS.

The first bit of the representation is the sign bit, with a 0-bit indicating positive and 1-bit indicating negative. Since JS has both +0 and -0, I am guessing that “0” as a number is positive and thus begins with a 0-bit, which would produce no change under an XOR. I don’t see how the above checks if the integer is positive or negative. I entered -1^0 into a node repl and got -1, which suggests that the above doesn’t check for negative integers. We may have gotten lucky in the exercise, since no negative integers were provided in realNumberArray.

I am still trying to figure out how the rest of it works, such as how this checks against fractions. When I type 1.5^0 into the node repl, I get 1, so I know it is working, but this isn’t making sense to me. This calculator suggests that 0 is represented as all 0-bits in IEEE-754, so I don’t understand how it is testing anything under an XOR.

This is an interesting solution.

1 Like

try const squaredIntegers = arr.filter(x=>(~~x)===x).map(x=>x*x);

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
// change code below this line
const squaredIntegers = arr.filter(x => x>0 && x%2===0).map(x =>
x*x);
// change code above this line
return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
that’s my solution and it works

This is an old thread which has been necrobumped, but I’ve just seen your question

Bitwise operators in JS actually coerce and truncate their arguments to 32 bit signed integers first before combining into 64 bit js numbers which is the trick being used to test if a value is an integer in that snippet

That being said, it’s not particularly great as it fails for ints larger than 32 bit and is relatively pointless considering we have Number.isInteger which is AFAIK more robust

thanks for your advice .

type or paste code here
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
  "use strict";
  // change code below this line
  const squaredIntegers = arr.filter((x)=>(x>0&&x%1===0)).map(x=>x*x);
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

I have understood everything up to this point. But this is so poorly explained, and seems to me like it’s gone straight into the deep end. It hasn’t built up or given me a chance to learn it. I look at the solution and literally have no idea what’s going on

4 Likes

Hey Guys I just need to know about the solution for this Quiz I just created a solution but using Ternary Operator and map() function but can’t work with me ::
this is the solution I came up with ::

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
  "use strict";
  // change code below this line
  const squaredIntegers = arr.map( (num) => num > 0 && num % parseInt(num) === 0 ? num * num : num);
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);
1 Like

My answer ended up being:

const squaredIntegers = arr.filter((num) => (num % 2 == 0 && num > 0)).map(x => x * x);

Figuring out what needed to happen wasn’t as difficult as trying to figure out how the syntax went to do it. After realizing my error with Math.sqrt, Math.pow didn’t work because I couldn’t seem to squish it in there anywhere correct. Coming here for help confirmed I was, once again, overthinking it. The dot notation can just keep going like that? That’s awesome!

1 Like

hey good let me show you my solution:
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 = realNumberArray.filter((arr) => arr !== 5.6 && arr !== 3.14 && arr !== -9.8 && arr !== 8.34 && arr !== -2).map( (arr) => Math.pow(arr, 2) )

// change code above this line

return squaredIntegers;

};

// test your code

const squaredIntegers = squareList(realNumberArray);

console.log(squaredIntegers);

Note: first I dismiss the negative numbers and decimal number, until get 4, 42, 6.
second I used a => funcion with map to get the squared integers.

I hope helps.

thanks

In general, it’s not a good idea to tailor things to your input too much - we want our code to run across a broad range of values, not just the specific values handed to us.

What would happen to your code if we added 3.2 to realNumberArray? What if we didn’t know what numbers were in realNumberArray at all?

It’s better to filter based on what a positive integer is, rather than what values our input array has i.e. arr >= 0 && arr % 1 === 0

1 Like