JavascriptGrains Exercise Fails

Test Failure
Error: expect(received).toEqual(expected) // deep equality

Expected: “18446744073709551615”
Received: “18446744073709551616”

How do I fix this? The other 10 tests pass. “deep equality” indeed.

The two numbers are different (the last digit is different). Is your code out by 1?

Can you show us your code so far?

export const square = (sqr) => {
    return total(sqr,'s')
};

export const total = (sqr=64,all='t') => {

    // Note that this is not a true total function. If the sqr number < whole
    // board it returns only what's on that square...if it is whole board,
    // i.e. 64, one more doubling == 2times the sum of all previous squares
    
 if(sqr < 1 || sqr > 64) throw new Error('square must be between 1 and 64')
 let two=2, pwr = sqr
 if (pwr != 64 || all == 's') pwr = pwr-1
 if(pwr>54) { pwr=BigInt(pwr); two=2n; pwr=BigInt(pwr) } 
 return two**pwr
};

So I just let ** do the computation–it otherwise seems to handle BigInts without any problem–as you see, all squares > 54 use BigInts.

Ok…I took a cold walk and I think I see it now–since the first square only has 1 on it, the final doubling is going 1 beyond the sum so far, so I need to subtract a 1…please correct me if this explanation is wrong–it will work, but maybe that’s just because I see the answer and now I can force it

Your function is called total but it doesn’t really seem to calculate the total number of grains. You are always returning a power of two: return two**pwr. Meaning you are most likely trying to calculate the number of grains that each square has.

I suggest that you put that logic inside the square function, and inside the total function see how you can re-use the square function to calculate the total number of grains.

Edit: Also, please take a look at the tests and see how they are calling the total and the square functions.

I call the function ‘total’ so that I can use it to produce results that the tests can find, but I twist it to my own purposes so it does both individual and final sum for me with almost the same code. i.e.-- the cumulative total is simply the total for one square too far, and I see now that that is one too many, because another doubling will produce an even number, and the cumulative total, starting with 1 grain on square one must produce an odd total

I’ve now fixed it by subtracting the extra 1 and it passes all tests. Thanks to people for looking at it:

export const square = (sqr) => {
return total(sqr,‘s’)
};

export const total = (sqr=64,all=‘t’) => {

// Note that this is not a true total function. If the sqr number < whole
// board it returns only what's on that square...if it is whole board,
// i.e. 64, one more doubling == 2times the sum of all previous squares

if(sqr < 1 || sqr > 64) throw new Error(‘square must be between 1 and 64’)
let two=2, pwr = sqr
if (pwr != 64 || all == ‘s’) pwr = pwr-1
if(pwr>54) { pwr=BigInt(pwr); two=2n; pwr=BigInt(pwr) }
let r=two**pwr
return (all == ‘t’) ? r-1n : r
};

Except that the code never calculates the total. It just so happened that the total was very close to the number of grains on a individual square, and you manually adjusted it for the total.

Perhaps, mathematically, this way of totaling fails at some point, but I’ve tested it for the number 123456 (one more power of 10 and my sloppy code would take forever to run) and for several ‘random’ smaller numbers, and that solution works each time.

It never fails. Search online to see why it always works.

After your reply I did look it up and saw that it was always true…so that is my total; yes, the code is somewhat clumsy; I’ve now looked at Community Solutions and seen that noone else bothers to use ordinary integers up to Readme’s 54, but since I was writing functions as specified by the test suite, I also followed Readme’s breakpoint for using BigInt, which I presume has some overhead cost