user3322858
user3322858

Reputation: 43

why my alert in the function not working??

The code should receive a sentence / string and print it in reverse, if word or letter that wrote in the filter contained within the word belongs to a string - the word will not print.

The question why my check alert "hi" not working?? tnx!

<html>
<head>  
  <script>
function myfunc()  {

alert ("hi"); 
var count=0; 
var phrase= document.getElementById('phrase').value;
var filter = document.getElementById('filter').value;
var arrReverse = phrase.split(" ").reverse(); 

for (i=0; i<arrReverse.length; i++) {
  if (arrReverse[i].search(filter)==-1) {
    if (i%2==0) {
      document.getElementById('words').innerHTML="<span class="word"><u>"arrReverse[i]"</u><span>";
    } else { 
      document.getElementById('words').innerHTML="<span class="word">"arrReverse[i]"<span>";
    }
  } else if (arrReverse[i].search(filter)!=-1) { count++; }

  if (count>0) {
    document.getElementById('count').innerHTML="<span class="count">"count "word(s) filtered out <span>";
  }
}

  </script>
</head>

<body >
  <h1>Sentence Reverser!</h1>

  <div> Phrase: <input id="phrase" type="text" size="40"/></div>
  <div> Filter:    <input id="filter" type="text" size="10"/></div>

  <div><button id="go" onclick="myfunc()"> Go! </button></div>

  <div id="words"></div>
  <div id="count"></div>

</body>
</html>

Upvotes: 0

Views: 212

Answers (1)

juvian
juvian

Reputation: 16068

Be careful with "" quotes and '' quotes:

.innerHTML="<span class="word"><u>"arrReverse[i]"</u><span>"; // wrong


.innerHTML="<span class='word'><u>"+arrReverse[i]+"</u><span>"; //right

For further reference check:

http://www.w3schools.com/js/js_obj_string.asp

Upvotes: 2

Related Questions