Daily Coding Challenge - Infected

Tell us what’s happening:

My code is returning the correct results for all test cases except for:

  1. infected(25) should return 5217638.

I am getting 5217728. Is anybody else having this problem?

Your code so far

function infected(days) {
  let infected = 1;

  for (let day = 1; day <= days; day++) {
    infected *= 2;

    if (day % 3 === 0) {
      let patched = Math.floor(infected * 0.2 + 0.5)
      infected -= patched;
    }
  }

  return infected;
}

console.log(infected(25))

Your browser information:

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

Challenge Information:

https://www.freecodecamp.org/learn/daily-coding-challenge/2025-11-02

Round the number of patched computers up to the nearest whole number.

Is that what you are doing here?

That’s correct. I’ve also tried using parseInt((infected * 0.2).toFixed(0)), as well as rewriting in Python and using the round() function, but always with the same result.

Math.floor() is not rounding the number up.

That’s why we add the 0.5!

Or you could search for a more appropriate function because what you are doing is a crappy hack.

As I mentioned, I have tried this using Number.toFixed() as well as Python’s round() functions. All of these methods round up to the nearest whole number. By the way, adding 0.5 to a floating point number and truncating the result is pretty much standard practice for rounding up, especially in languages that don’t provide a rounding function.

Using ceil is the standard practice for rounding up. Adding 0.5 and rounding down should only be used if the language refuses to support a proper ceil function. Hacks are asking for errors and in this place you’re asking for the potential for floating point rounding issues as you need to rely upon correctly adding 0.5

Generally, you should only do a hack if you cannot do the actual thing you want to do directly

Thanks - that solved it. I misunderstood the instruction for rounding up.