multiplesOf3and5 - Error in one of the tests

Tell us what’s happening:
One of the test is erroneous :
“multiplesOf3and5(1000)` should return 233168”
But that’s in fact the result for 999 !
The correct answer is 234168…

  **Your code so far**

function multiplesOf3and5(number) {
const mul15 = Math.floor(number / 15);
const res15 = number % 15;
let res;
if (res15 < 7) {
  if (res15 < 5) res = 3 + mul15 * 15;
  else if (res15 == 5) res = 8 + mul15 * 30;
  else res = 14 + mul15 * 45;
}
else {
  if (res15 < 10) res = 23 + mul15 * 60;
  else if (res15 < 12) res = 33 + mul15 * 75;
  else res = 45 + mul15 * 90;
}
return res + mul15 * 60 + 105 * mul15 * (mul15 - 1) / 2;
}

console.log(multiplesOf3and5(10));
  **Your browser information:**

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

Challenge: Problem 1: Multiples of 3 and 5

Link to the challenge:

The test is correct. I am looking for the bug in your code.

It would help if you could explain how your code is intended to work.

I checked with a loop.
And also developed the sequence in Excel…

You checks are not correct. There is a mathematical formula to compute the result.

And my code works for the other 15*n + 11

I believe you have an off-by-one error. I’m just trying to see where it is.

Ok, my code calculates 2 blocs :
Mul15 * complete blocs of 3, 5, 6, 9, 10, 12, 15.
And the remaining values (here 990 + 3, +5, +6, +9, +10)

What do you mean by ‘bloc’? It seems that you are trying to account for residue classes mod 15.

I mean : 1000 = 66*15 +10
My awnser is 229185 + 4983 = 234168

And look, these are all the values calculated by Excel :

975 3 223278
975 5 224258
975 6 225239
975 9 226223
975 10 227208
975 12 228195
975 15 229185
990 3 230178
990 5 231173
990 6 232169
990 9 233168
990 10 234168
990 12 235170
990 15 236175

Your calculations in excel are wrong.

There is a simpler approach.

  1. Compute the number of multiples of 3

  2. Compute the number of multiples of 5

  3. Compute the number of multiples of 15

  4. Compute 1. + 2. - 3.

Using the correct mathematical formulae yields the correct value.

No I’m right :
let tot = 0;
for (let i = 0; i <= 1000; i++) if ((i%3) === 0 || (i%5) === 0) tot += i;
console.log(tot)
234168

I see the problem. Both of us can’t read instructions

Find the sum of all the multiples of 3 or 5 below the provided parameter value number .

Yes, that’s precisely what I did here.
And look…
for (let i = 0; i <= 999; i++) if ((i%3) === 0 || (i%5) === 0) tot += i;
233168

below

You have an off-by-one. You are using the wrong upper bound.

Ooh, mother !
Thanks… sorry

1 Like

We’ve all been there. I’ve been there most recently 18 minutes ago :smiley:

How to fail stupidely at an hiring interview (lol)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.