Back to Freecodecamp

Challenge 238: Digit Rotation Escape

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69b559d2903b9e4afe9075f8.md

latest1.1 KB
Original Source

--description--

Given a positive integer, determine if it, or any of its rotations, is evenly divisible by its digit count.

A rotation means to move the first digit to the end. For example, after 1 rotation, 123 becomes 231.

  • Check rotation 0 (the given number) first.
  • Given numbers won't contain any zeros.
  • Return the first rotation number if one is found, or "none" if not.

--hints--

getRotation(123) should return 0.

js
assert.equal(getRotation(123), 0);

getRotation(13579) should return 3.

js
assert.equal(getRotation(13579), 3);

getRotation(24681) should return "none".

js
assert.equal(getRotation(24681), "none");

getRotation(84138789345) should return 6.

js
assert.equal(getRotation(84138789345), 6);

--seed--

--seed-contents--

js
function getRotation(n) {

  return n;
}

--solutions--

js
function getRotation(n) {
  const str = String(n);
  const digitCount = str.length;
  let current = str;

  for (let i = 0; i < digitCount; i++) {
    if (Number(current) % digitCount === 0) return i;
    current = current.slice(1) + current[0];
  }

  return "none";
}