freeCodeCamp Challenge Guide: Self Describing Numbers

Self Describing Numbers


Solutions

Solution 1 (Click to Show/Hide)
function isSelfDescribing(n) {
  let digits = n.toString().split(""); // Array of digits
  // Check that every digit is as frequent as the value in that position
  return digits.every(
    (targetCount, position) =>
      // Check number of matching digits for current position
      targetCount == digits.filter(num => num == position).length
    );
}
1 Like