javascript - it says "to read a single character from user and check whether the character is present in the sentence or not?please help me -
var sentence = window.prompt('please enter short sentence describing home',''); var character = window.prompt('please enter single character',''); var count = 0; (var = 0; < sentence.length(); i++) if(sentence.charat(i) == character) // count++; document.write('the character' + character + 'is appearing ' + count + 'times in sentence'); else document.write('character not found.please try again');
get character , sentence , following:
var sentence = window.prompt('please enter short sentence',''); var character = window.prompt('please enter single character',''); var count = 0 for(var = 0; < sentence.length; i++){ if(sentence[i] == character){ count+=1; } } if(count > 1){ alert("the character appeared "+count+" times"); //to fix 'one times' }else if(count == 1){ alert("the character appeared 1 time"); //to fix 'one times' }else{ alert("the character did not appear in sentence"); }
first values, , loop on sentence
, each character
, if matches desired character
, add count
. @ end, if count
greater 0
, alert amount of times, , if is 0
, alert not in sentence.
Comments
Post a Comment