Daily Coding Challenge - Perfect Cube Count - Error Test Case

Tell us what’s happening:

This test case should return 1, but the challenge description wants 2 to be returned.
We are starting from number 3.

// countPerfectCubes(3, 30) should return 2.

Your code so far

function countPerfectCubes(a, b) {
  let [maxNum, minNum, count] = [Math.max(a, b), Math.min(a,b), 0]
  
  for(let i = minNum; i <= maxNum; i++){
    const cube = i ** 3
    if(cube > maxNum) break
    if (cube <= maxNum && cube >= minNum) count++
  }

  return count
}

countPerfectCubes(3, 30)// Should return 1, but challenge test case expected 2

// countPerfectCubes(1, 30)
// countPerfectCubes(30, 0)
// countPerfectCubes(-64, 64)
// countPerfectCubes(9214, -8127)

Your browser information:

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

Challenge Information:

Daily Coding Challenge - Perfect Cube Count

https://www.freecodecamp.org/learn/daily-coding-challenge/2026-03-03

there are 2*2*2 = 8 and 3*3*3 = 27
2 seems correct, between 3 and 30 there is 2 perfect cubes, 8 and 27

Given two integers, determine how many perfect cubes exist in the range between and including the two numbers

@ILM The instruction says “In the range” and “Including the two numbers”, i.e., 4 - 29, including 3 and 30 for countPerfectCubes(3, 30), so the number 2 is not in the range, nor the min or max.

That is why this test case countPerfectCubes(1, 30) passes and returns 3 perfect cubes cause we have 1, 2, and 3 in the range.

You seem confused on what a perfect cube is. When we say that 8 = 2*2*2, we say that 8 is a perfect cube because it is the result of a number multying itself in that way. 8 needs to be in the range to be counted, not 2.
Otherwise every number would need to be counted, any integer can create a perfect cube, 4*4*4 = 64, 5*5*5 = 125 and so on

the perfect cubes in the range 3-30 are 8 and 27

2 and 3 are not perfect cubes, there isn’t an integer n that gives those numbers when you calcualte n*n*n

no, that is 3 because you have 1, 8 and 27, which come from 1*1*1, 2*2*2 and 3*3*3

and when you go down to a range that starts at 0, the other perfect cube is 0 = 0*0*0

in the range -64, 64 there are 9 because -64 = -4*-4*-4, -27=-3*-3*-3, -8=-2*-2*-2, -1=-1*-1*-1, 0=0*0*0, 1=1*1*1, 8=2*2*2, 27=3*3*3, 64=4*4*4

@ILM Okay, understood, thanks.

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