Back to Freecodecamp

Challenge 225: No Consecutive Repeats

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

latest1.2 KB
Original Source

--description--

Given a string, determine if it has no repeating characters.

  • A string has no repeats if it does not have the same character two or more times in a row.

--hints--

hasNoRepeats("hi world") should return true.

js
assert.isTrue(hasNoRepeats("hi world"));

hasNoRepeats("hello world") should return false.

js
assert.isFalse(hasNoRepeats("hello world"));

hasNoRepeats("abcdefghijklmnopqrstuvwxyz") should return true.

js
assert.isTrue(hasNoRepeats("abcdefghijklmnopqrstuvwxyz"));

hasNoRepeats("freeCodeCamp") should return false.

js
assert.isFalse(hasNoRepeats("freeCodeCamp"));

hasNoRepeats("The quick brown fox jumped over the lazy dog.") should return true.

js
assert.isTrue(hasNoRepeats("The quick brown fox jumped over the lazy dog."));

hasNoRepeats("Mississippi") should return false.

js
assert.isFalse(hasNoRepeats("Mississippi"));

--seed--

--seed-contents--

js
function hasNoRepeats(str) {

  return str;
}

--solutions--

js
function hasNoRepeats(str) {
  for (let i = 1; i < str.length; i++) {
    if (str[i] === str[i - 1]) {
      return false;
    }
  }
  return true;
}