Jump to content

.London Domains - £4
Photo

remove decimal numbers from array


  • Please log in to reply
3 replies to this topic

#1 historygirllfc

historygirllfc
  • Hefty Member
  • Advanced Member
  • 634 posts

Posted 18 August 2018 - 15:44

So I'm slowly working my way through FCC curriculum and currently on ES6 section.

 

This is as far as I have got so far:

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(number => number > 0 && parseInt(number)???);
  console.log(squaredIntegers);
  // change code above this line
  return squaredIntegers;
};

Challenge is to take the integers from the array and store in squaredIntegers array. Question is how do I actually filter out the decimal numbers... Am I thinking along the right lines using parseInt?


  • 0

YNWA


#2 DigitalSquid

DigitalSquid
  • Overlord
  • Squidministrator
  • 1,051 posts

Posted 19 August 2018 - 09:36

https://developer.mo...umber/isInteger


  • 1

More than happy to help for free but if you want to show some love back, either give me an up-vote or buy me a beer!

www.digitalsquid.net
@Digital_Squid


#3 historygirllfc

historygirllfc
  • Hefty Member
  • Advanced Member
  • 634 posts

Posted 27 August 2018 - 14:16

Thanks for the reply however that doesn't appear to do anything... It just continually sets isInteger is not a function... what am I doing wrong? When I sometimes think I'm getting better at JS when I struggle with this kind of stuff I think perhaps that isn't the case after all.

 

I cheated and looked up the answer which is:

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(number => number > 0 && number % parseInt(number) === 0 ).map((number) => Math.pow(number, 2) ); 
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);

I would have never done that... I'm sure there are multiple ways of approaching it but Math.pow is not a way I would have thought of and I don't remember that being anywhere on FCC stuff I have done so far. Not sure it is really helping me I think I'll going back to Treehouse when funds allow.

 

But anyway in terms of using .isInteger how would I use that in the code above?


  • 0

YNWA


#4 Lyndsey

Lyndsey
  • Supporter+
  • Standard Member
  • 404 posts

Posted 31 August 2018 - 08:58

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squaredIntegers = realNumberArray.filter(num => Number.isInteger(num));

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users