Home > Back-end >  Button calls entirely different function than what its supposed to
Button calls entirely different function than what its supposed to

Time:10-06

What i want to happen is when i click the button after typing yes, it says "OLD MAN: thats good to hear, whats your name?" but instead it calls a different function saying the else answer for the question that has not been added yet. This is probably me being very dumb, because im new and bad at html/jss but if anyone sees why this is happening and let me know it would be greatly apreciated

the html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet"type="text/css"/>
    <script src="script.js"></script>
  </head>
  <body onload="onload()">  
    <center>
    <h1 class="Title"> Text Adventure HMTL </h1>
    <h5> Please write your answer directly how it was written after the question</h5>
    <br><br>
    </center>
    <p id="currentQuestion">
    <p id="givenAnswers"></p>

    <input type="textbox" id="answer" placeholder="Type your answer here">
     <input  id="enter" type="button" onclick="wakeUp()" value="enter">
    
  </body>
</html>

the js:

var answers="Yes<br>No"
var name=""
function onload(){
document.getElementById("currentQuestion")    .innerHTML=currentQuestion=currentQuestion
document.getElementById("givenAnswers").innerHTML=answers
 

}


function wakeUp(){
 document.getElementById("currentQuestion").innerHTML=currentQuestion
 document.getElementById("givenAnswers").innerHTML=answers
   if(document.getElementById("answer").value=="No" || document.getElementById("answer").value=="no" )
  {
   
   currentQuestion="You did not wake up, the game has ended, please restart"
   
   answers=""
    document.getElementById("answer").value=""
   onload()
   document.getElementById("enter").onlclick = "dead()"
   
   
   }
   else if(document.getElementById("answer").value=="Yes" || "yes" ){
    
   currentQuestion="OLD MAN: Good morning, how are you feeling?"
   answers="Good<br>Bad<br>Else"
    document.getElementById("answer").value=""
   
   document.getElementById("enter").onlclick = howUFeel()
   }
   else{
     document.getElementById("answer").value=""
   }
}

function dead(){
  currentQuestion="You have died, please restart"
  document.getElementById("currentQuestion").innerHTML=currentQuestion
  document.getElementById("answer").value=""
  onload()
}

function howUFeel(){
 if (document.getElementById("answer").value ==  "else" || "Else" )
 {
 currentQuestion="What do you mean by else? care to elaborate?"
 answers="No"
 onload()
 
  document.getElementById("answer").value=""
 }
  else if(document.getElementById("answer").value ==  "good" || "Good" ){
    currentQuestion="OLD MAN: Im Happy to hear it, do you by chance remember your name?"
    
  }
  else{


    
  }
} 

Here you can test it and see what it does: https://idkwthisgoingon.kitten3604.repl.co/

CodePudding user response:

Edited Answer

You are using if incorrectly: if(something == option1 || option2) doesn't work like you think it does. do:

if(document.getElementById('answer').value == option1 || document.getElementById('answer').value == option2)

in your checks.

if(something == string1 || string2) will always return true because a non-empty string is always considered true when converting into a boolean therefore the second condition in the if statement, which is string2, will be true

  • Related