1

I am working on my assignment where I have to ask a user to type specific letter (capital), if not then I am not supposed to process the input data

Tried to use if and specified which letters I am expecting

function myFunction() {
  var bokstav, r4;
  bokstav = document.getElementById("tipping").value;
  if (bokstav !== "H" || bokstav !== "U" || bokstav !== "B") {
    r4 = "Du har ikke brukt stort bokstav eller tastet ugyldig tegn";
  } else if (bokstav = "H") {
    r4 = "Hjemme";
  } else if (bokstav = "U") {
    r4 = "Uavgjort";
  } else {
    r4 = "Borte";
  }
  document.getElementById("demo").innerHTML = r4;
}
<p>Type H/B/U:</p>

<input id="tipping" value="H" type="text" />

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

I expect the user to type just H, U or B. Then if the "H" is typed then "Hjemme" is displayed on the screen, if "B" is typed then to show "Borte" on the screen and so on. Otherwise ask the user to type only H, U, B ( capital)

Siddharth Pal
  • 1,408
  • 11
  • 23
Dimarco
  • 35
  • 6
  • Have you tried to debug the code yourself using `debugger;` in your browser? – palaѕн Sep 11 '19 at 16:15
  • I don´t know how to do that... – Dimarco Sep 11 '19 at 16:22
  • @Dimarco - You have to use `&&` operator instead of `||` here. And in `if/else` use `===` for comparision instead of `=`. This will fix the issue. – Nikhil Sep 11 '19 at 16:24
  • I have just tried and it didn´t work: var b,r4; b= document.getElementById("tipping").value); if (b!==="H"&&b!==="U"&& b!==="B") { r4="Du har ikke brukt stort bokstav eller tastet ugyldig tegn"; } else if (b==="H"){ r4="Hjemme"; } else if (b==="U"){ r4="Uavgjort"; } else { r4="Borte"; } document.getElementById("demo").innerHTML = r4; } – Dimarco Sep 11 '19 at 16:31
  • @Dimarco - `===` is used for strict equality and `!==` for strict inequality (not `!===`). – Nikhil Sep 11 '19 at 19:21

0 Answers0