freeCodeCamp Challenge Guide: Problem 29: Distinct powers

Problem 29: Distinct powers


Solutions

Solution 1 (Click to Show/Hide)
function distinctPowers(n) {
  let powers = {};
  for (let i = 2; i <= n; i++) {
    for (let j = 2; j <= n; j++) {
      powers[Math.pow(i, j)] = true; // Repeated instances will not add additional key
    }
  }
  return Object.keys(powers).length;
}