1
 <script>
  var arr = [];

  function repeater(str){
    for (var i=0; i<str.length;i++)
      {arr.push(str[i])}
      arr.sort()
    for (var g=0;g<arr.length;g++) {
    if (arr[g]==arr[g+1])
     {return false}

      else {return true}

    }
  }

document.write(repeater("jrtgrt"))
    console.log(arr)
</script>

Create a function that takes a string and returns either true or false depending on whether or not it has repeating characters.

The array is working by console, but the second part doesn't seem to be running.

3 Answers 3

1

Your loop is terminating after the first comparison because either way return is called.

<script>
    var arr = [];

    function repeater(str) {
        for (var i = 0; i < str.length; i++) {
            arr.push(str[i])
        }
        arr.sort()
        for (var g = 0; g < arr.length - 1; g++) {
            console.log(arr[g], arr[g + 1])
            if (arr[g] == arr[g + 1]) {
                return true
            }
        }
        return false;
    }

    document.write(repeater("12s35sd46"))
    console.log(arr)
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

There are a few ways to simplify your code.

You don't need a for loop to create an array from a string. You can use the split function.

arr = str.split("");

Your entire function can literally be simplified to this using a regular expression.

function repeater(str) {
    return /(.).*\1/.test(str);
}

Here's an explanation of each part of the regular expression:

(.) any character, capture to use later
.* any character any number of times
\1 first captured character

2 Comments

Hi thank you for this. But I just started learning js and I would rather to stay at simple and basic methods for now.
@JiangYuxin Perfectly reasonable but the suggestion to use str.split("") is a good one and would improve your code. It's a good idea to learn the array and string helpers early - they are simple and helpful.
0

An approach might not necessarily rely on an array's sort method. One, for instance, could stepwise shorten the list of characters and then, via an array's some method, determine whether the current character has a duplicate counterpart in the yet remaining list of characters. Thus one also keeps the amount of iteration cycles low ... example ...

function hasDuplicateChars(stringValue) {
    var hasDuplicate = false;

    var charList = stringValue.split('');
    var char;

    // continue taking the first entry of `charList` while mutating the latter.

    while (!hasDuplicate && (char = charList.shift())) { // `char` either will be a string or a undefined value.

      hasDuplicate = charList.some(function (listItem) { return (char === listItem); })
    }
    return hasDuplicate;
}

console.log('hasDuplicateChars("") ? ', hasDuplicateChars(""));
console.log('hasDuplicateChars("x") ? ', hasDuplicateChars("x"));
console.log('hasDuplicateChars("abcdefghijklmnopqrstuvwxyz") ? ', hasDuplicateChars("abcdefghijklmnopqrstuvwxyz"));

console.log('hasDuplicateChars("TheQuickBrownFox") ? ', hasDuplicateChars("TheQuickBrownFox"));
console.log('hasDuplicateChars("Hallo, world.") ? ', hasDuplicateChars("Hallo, world."));
console.log('hasDuplicateChars("  ") ? ', hasDuplicateChars("  "));
.as-console-wrapper { max-height: 100%!important; top: 0; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.