<41.png">

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

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

#1

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

According to MDN:

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


#3

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


#4
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);

#5

so hard to understand…


#6

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.


#7

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


#8

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


#9

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


#10

thanks for your advice .


#11
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);

#12

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


#13

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);