SAI PALADUGU
SAI PALADUGU

Reputation: 51

javascript letter character finder infinite loop glitch

My task was to create a while loop that counts the number of letter characters in a sentence written by the User. This function would only count the number of letter characters, no special characters. My issue it that when I add special characters (including numbers), in a some random given order (it works sometimes), it crashes my browser (it will crash yours too, be careful!). I don't know what is causing this.

var chocolate = false;
var count = 0;
var characters = 0;
function letterCounter(){
  var sentence = document.getElementById('sentence').value;
  while(!chocolate){
    if (count==sentence.length) {
      var chocolate = true;
    }
    else if(sentence[count]=="a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"){
      count++;
      characters++;
    }
    else {
      count++;
    }
    document.getElementById('replaceTwo').innerHTML = characters;
}
}
Function 2: letter count finder<br>
      <textarea id="sentence"></textarea><br>
      <button onclick="letterCounter()">Find letters</button>
      <div id="replaceTwo"></div>

Upvotes: 2

Views: 142

Answers (3)

Akashdeep Singh
Akashdeep Singh

Reputation: 408

The problem is the variables characters, chocolate and count have been declared outside of the function and the function operates on them. So, the function goes into an infinite loop after the first call if there are leading special characters in the input.

function letterCounter(){
    var sentence = document.getElementById('sentence').value;
    var chocolate = false;
    var count = 0;
    var characters = 0;
    while(!chocolate){
        if (count==sentence.length) {
	        chocolate = true;
        }
        else if(["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"].indexOf(sentence[count])!==-1){
	        count++;
            characters++;
        } else {
	        count++;
        }
 
    }
    console.log(characters)
}
Function 2: letter count finder<br>
      <textarea id="sentence"></textarea><br>
      <button onclick="letterCounter()">Find letters</button>
      <div id="replaceTwo"></div>

This won't crash and incorporates @manuel-otto's fix.

Upvotes: 3

Pingolin
Pingolin

Reputation: 3417

The problem with your code is that you are declaring a new variable chocolate inside your if:

if (count==sentence.length) {
  var chocolate = true;
}

Here is a working solution that won't crash your browser :)

var btn = document.getElementById('btn');
var sentence = document.getElementById('sentence');
var count = 0;
var re = /[a-z]/i;

btn.addEventListener('click', function() {
  count = 0;

  for (var i = 0; i < sentence.value.length; i++) {
    if (re.test(sentence.value[i])) {
      count++;
    }
  }

  document.getElementById('replaceTwo').innerHTML = count;
});
<p>Function 2: letter count finder</p>
<textarea id="sentence"></textarea>
<button type="button" id="btn">Find letters</button>
<div id="replaceTwo"></div>

Upvotes: 1

Manuel Otto
Manuel Otto

Reputation: 6540

This

sentence[count]=="a","b","c",...

will not work as expected.

You will have to put those letters into an array check if that array contains the current letter:

["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"].indexOf(sentence[count])!==-1

Upvotes: 2

Related Questions