0

I am trying to add multiple values for the same output "Maths book". I think I am missing something basic on how to format values in brackets.

<script type="text/javascript">
  <!--
  var book = "maths";
  if (book == "history") {
    document.write("<b>History Book</b>");

    /*How to add more options below besides "maths" so the output will be "Maths
    Book". I want the output to be "Maths Book" also when user enters 
    mathemathics, mata, maths book...

    Do I need more else if lines or I can put additional values in in brackets*/


  } else if (book == "maths") {
    document.write("<b>Maths Book</b>");
  } else {
    document.write("<b>Unknown Book</b>");
  }
  //-->
</script>
<p>Set the variable to different value and then try...</p>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
dentist
  • 186
  • 1
  • 1
  • 11
  • 1
    You’re looking for the [logical OR operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators), but you should also find a newer resource for learning JavaScript if your current one is old enough to recommend ``. – Ry- May 04 '18 at 12:33
  • You mean something like `if (book.toLowerCase().indexOf("math") !=-1)` – mplungjan May 04 '18 at 12:33
  • I am using tutorialspoint.com PDF JS tutorial. What is wrong with `` – dentist May 04 '18 at 13:05

1 Answers1

0

Try following script and JSFiddle: https://jsfiddle.net/19d7g8f0/2/ will help

function addBook(book){
   if( book == "history" || book=="archive"){
      document.write("<b>History Book</b>");
   }else if( -1 != ["maths","mathematics","arithmetics"].indexOf(book.toLowerCase()) ){
      document.write("<b>Maths Book</b>");
   }else{
      document.write("<b>Unknown Book</b>");
   }
}

addBook("Maths");
addBook("mathematics");
addBook("Arithmetics");
Miroslav Savovski
  • 2,300
  • 2
  • 10
  • 12