Home>

ruby ​​on rails
Use gem gon to bring variables to JS.

I currently have an if expression, but the wrong answer is displayed.
I have data up to console.log, but if is not judged.

I'm sorry for the confusing explanation, but I hope you can borrow wisdom.
Nice to meet you.

<form name = "answer">
 <p><input type = "text" name = "text" row = "1" cols = "15" value = "" placeholder = "Enter answer" id = "textData" class = "answer__text">
 </p>
</form>
<input type = "submit" name = "btn" value = "answer" id = "sendBtn">
$(function () {
 $("# sendBtn"). click (function () {
  const getData = document.answer.text.value;
  console.log (getData)
 if (getData == gon.ansewer) {
   document.getElementById ("area-ans"). innerText = "Correct";
   document.getElementById ("area-ex"). innerText = gon.explanation;
 } else {
Document.getElementById ("area-ans"). InnerText = "Incorrect";
 Document.getElementById ("area-ex"). InnerText = gon.explanation;
  };
 });
});
  • Answer # 1

    if statement

      

    I currently have an if expression, but the wrong answer is displayed.

    ifis a "sentence", not an "expression".

    if ... else-JavaScript | MDN

    Reproducible code
    if (getData == gon.ansewer) {

    There is no initialization process forgetData,gon.ansewerin the posted code, so we have no way of knowing the situation.
    You needreproducible codeto communicate correctly to respondents.

    How to write minimal, self-contained, verifiable sample code-Help Center-Stack Overflow

    Carving

    To narrow down the cause,separationis required.

    console.log (typeof gon.ansewer, typeof getData, typeof gon.ansewer === typeof getData);// undefined string false
    console.log (JSON.stringify (gon.ansewer), JSON.stringify (getData), gon.ansewer === getData);// undefined "sample" false

    This result means one of the following:

    gondoes not have propertyansewer

    gon.answercontains aundefinedvalue

    If you execute the following code, you can narrow down the cause to one.

    console.log (typeof gon, 'answer' in gon, Object.prototype.hasOwnProperty.call (gon, 'ansewer'), Object (gon) === gon, JSON.stringify (gon));
    console.dir (gon);

    Re: zigrable-san

  • Answer # 2

    The "sample" doesn't convey important points.
    Is it understood that it is important to be aware of the type when using "=="?

    The difference between "==" and "===",
    I think that it will be solved if we investigate truthy and falsy properly.

    Difference between JavaScript ==== and ==

    Truthy-MDN Reference

  • Answer # 3

    Turolinks has been deleted, and if it was deleted, it was possible to judge whether it was correct or incorrect.