Can't figure out why this is failing

Tell us what’s happening:
I’m not sure why the first test is failing, I’ve tried returning the answer as a number and as a string, but neither will pass the first test (a number will pass the second test). Am I missing something obvious??
Thanks :slight_smile:

Your code so far


function largeSum(arr) {
// Ensuring any strings are parsed to numbers
for (let i=0; i<arr.length; i++) {
  arr[i] = parseInt(arr[i])
}
// Finding the sum of the array
let x = arr.reduce((a, b) => a+b)

// Finding the first 10 digits
while (x > 9999999999) {
  x /= 10
}
let y = Math.floor(x)
let z = y.toString()
console.log("x:", typeof(x), x)
console.log("y:", typeof(y), y)
console.log("z:",typeof(z), z)

return y;
}

// only change code above this line

const testNums = [
'37107287533902102798797998220837590246510135740250',
'46376937677490009712648124896970078050417018260538'
];

largeSum(testNums);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.

Challenge: Problem 13: Large sum

Link to the challenge:
https://www.freecodecamp.org/learn/coding-interview-prep/project-euler/problem-13-large-sum

that number is too big to handle. You cannot parseInt on that, JS’ Number is a 32 bit floating point so only handles numbers up to 253 -1, that’s the point of the exercise. Use BigInt

1 Like

I don’t get it. I’ve read the MDN, watched a youtube vid. I can get it to work on the Dev Console, but if I type something like ‘3n’ in FCC, it makes a fuss.

  // Ensuring any strings are parsed to numbers
  for (let i=0; i<arr.length; i++) {
    arr[i] = BigInt(arr[i])
  }
  console.log(arr)  //Output [ {}, {} ]
  ...

Are BigInts objects? How can I get at the values?

let myBigInt = 3n;

This throws an error in FCC.

1 Like

It not going to show you the number that way because it’s still too big, BigInt is like a box you can put huge numbers in, but it’s opaque. Here, you don’t need to see the whole number, you only need to add up. Once that’s complete then you can use my_big_int.toString(), take the first 10 characters then parse it to a normal number

Edit: hmm, if FCC doesn’t support BigInt that’s a problem. Try it without the literal syntax as you can’t use that anyway (you aren’t defining big numbers, they’re already in the array in string form)

2 Likes

Yes, we’re not defining big numbers, I was just playing around to figure out what I can / can’t do with them. Thanks for trying to explain, I think most of my confusion is coming from that I am getting the same answer that is expected, but still failing the test – i.e. it is not obvious that the number is being rounded here parseInt(arr[i])

Maybe it would be better to try and get the last 10 digits rather than the first 10 (as they are more likely to be different). Or have a note / or better yet a test saying what datatype it is expecting as the result.

Anyway, I managed to get a solution using BigInt(), rather than Number(), given in the example answer. Thanks for your help :slight_smile: