What is wrong with my code? (Javascript)


#1

This is my code:

<!doctype html>
<html> 
<head> do you like Javascript? 
<title></title>
    

<script> 

function javarun ()
 { 
     var answer;  
     if (answer= "yes") 
     {
        alert ("that is great!");
     
     } else if (answer="no") 
        {
            alert ("That is too bad");
        } else { 
                    alert ("Invaild input");    
               
               }
    
 }
  
    </script>

</head>
<body> 
<input type= "text" id= "txtInput">
<input type="button" value ="Click Me" onclick= "javarun()"/                 
    
</body>
</html>

It always says “that is great” no matter what I enter. Whats wrong with my logic?


#2

Did you mean to type the text into the <title></title>?

answer is undefined.

Comparisons use ===

Missing a closing carat >

Study this example…

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Do you like Javascript?</title>
    <script>
    function javarun () { 
      var answer = document.querySelector('input').value.toLowerCase();  
      if (answer === "yes") {
        alert ("that is great!");
      }
      else if (answer === "no") {
        alert ("That is too bad");
      } else { 
        alert ("Invaild input");    
      }
    }
    </script>
  </head>
  <body>
    <input type="text" placeholder="Do you like Javascript?">
    <input type="button" value="Submit" onclick="javarun()">
  </body>
</html>

https://repl.it/NXtV


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.