如果不是,那么相同输出的更多变量(JS)

时间:2018-05-04 12:30:26

标签: javascript

我正在尝试为同一输出添加多个值"数学书"。我想我遗漏了一些关于如何格式化括号中值的基本知识。



<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

尝试以下脚本和JSFiddle:https://jsfiddle.net/19d7g8f0/2/将提供帮助

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");