Anonymous
Anonymous asked in 電腦及互聯網程式編寫及設計 · 7 years ago

java question 2

Consider the following

JavaScript code for showing the occurrences of the letter e in the

input word . Rewrite the code by using

while

loop.

var word, total;

word = window.prompt('please enter a word','');

total = 0;

for(var count=0; count<word.length; count= count+1)

{

if (word.charAt(count) == 'e')

{

total = total + 1

}

};

document.write('The number of occurrences of e in the word '

+ word +' is '+ total)

1 Answer

Rating
  • John
    Lv 7
    7 years ago
    Favorite Answer

    for..loop actual is the best to conduct this kind of work.

    semicolon are optional but use it with care, 99% of developers adding semicolon at the end of their code, why you not? with semicolon at the end you could put all statements in one line the code would be "minifierable".

    var word = window.prompt("Please enter a word");

    var total = 0;

    var i = 0;

    while (i < word.length) {

    if (word.charAt(i) == 'e') total++;

    i++;

    }

    document.write('The number of occurrences of e in the word ' + word + ' is ' + total);

    Source(s): blog.magnetiq.com/post/493356934/when-semicolons-are-not-optional-in-javascript
Still have questions? Get your answers by asking now.