-1

Even when I type in the correct response "Jesus wept." I continue to received the feedback, "Please try again." when I press the Click to check button. Can anyone point out my mistake?

    function check() {
      var w1 = document.getElementById("wordOne");
      var w2 = document.getElementById("wordTwo");
      if (w1 == "Jesus" && w2 == "wept.") {
        document.getElementById("feedack").innerHTML = "Well done!";
      } else {
        document.getElementById("feedback").innerHTML = "Please try again.";
      }
    }
<div>
  <input type="text" id="wordOne">
  <input type="text" id="wordTwo">
  <br>
  <input type="button" id="evaluate" value="Click to check" onclick="check()">
  <br>

  <h2 id="feedback"></h2>

</div>
Joseph
  • 1
  • 2
    there is a typo in your code. "feedack" – Lyes BEN Jan 02 '16 at 03:30
  • I.e Test 'w1.value'. – Mark Larter Jan 02 '16 at 03:35
  • `var w1 = document.getElementById("wordOne").value`, i suppose... – ankhzet Jan 02 '16 at 03:47
  • Interesting example, "Jesus wept." – Patrick Roberts Jan 02 '16 at 04:14
  • You should debug your code. Open devtools. Please a breakpoint on the line where you are doing the comparison. Type in `w1 == "Jesus"` and see the result, which will be `false`. To see why, type in `w1`, or examine the value of the variable `w1` in the "Scope" section, You will see that it is not the string "Jesus" as you expected, but rather an HTML `input` element. Then you can proceed to figure out how to get the value of an element. –  Jan 02 '16 at 04:26

3 Answers3

0
  1. You need to get the value of the HTML element returned.
  2. You had feeback when you meant feedback

function check() {
      var w1 = document.getElementById("wordOne");
      var w2 = document.getElementById("wordTwo");
      if (w1.value == "Jesus" && w2.value == "wept.") {
        document.getElementById("feedback").innerHTML = "Well done!";
      } else {
        document.getElementById("feedback").innerHTML = "Please try again.";
      }
    }
<div>
  <input type="text" id="wordOne">
  <input type="text" id="wordTwo">
  <br>
  <input type="button" id="evaluate" value="Click to check" onclick="check()">
  <br>

  <h2 id="feedback"></h2>

</div>
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
0

w1 and w2 are DOM elements. You have to get the value of the input using value .

Same question here

Community
  • 1
  • 1
Monkey
  • 121
  • 4
-1

You have to make sure that you include the period in the second input or your evaluation will not match. Since that is included in the text that you are checking for.

andre mcgruder
  • 1,120
  • 1
  • 9
  • 12