我有2个引号数组。我希望我的客户能够选择一种报价类型,也可以选择所需的报价数量

时间:2018-07-18 00:47:59

标签: javascript

我有2个数组。我希望能够选择报价的类型以及要显示的报价数量。可悲的是我的代码什么也没显示。我尝试调试,但未显示任何错误。分开时,if语句运行正常,但我无法获得选项输入来运行“ for”循环。

the HTML
<p>Choose a type of quote. You can Choose 'a' for travel information or 'b' wise sayings.</p>
      <form onsubmit=" mySayings()">
        Quote: <select  type="text"id="nQuotes" name="quote">
          <option value="a">a</option>
          <option value="b">b</option>
        </select>
      </div>
      <div>
        <form>
                        Number of quotes?:
                        <input id="quotesNeeded" type="number" min="1" max="5"/>
                        <input type="button" onsubmit="showQuote()" value="Submit"/>
        </form>
      </div>
      <div>
     <p id="demo"></p>
    </div>
    </div>

    The js
    //make the arrays
      var countries=["America", "Kenya", "Thailand"];
      var capitals = ["Washington", "Nairobi", "Bankok"];
      var currency = ["dollar","shilling", "baht"];
      var randomCountry = Math.floor(Math.random() * countries.length);
      var randomCapital = Math.floor(Math.random() * capitals.length);
      var randomCurrency = Math.floor(Math.random() * currency.length);

      var myQuotes=["Success is not final; failure is not fatal: It is the courage to continue that counts.-- Winston S. Churchill","It is better to fail in originality than to succeed in imitation.-- Herman Melville","The road to success and the road to failure are almost exactly the same.-- Colin R. Davis"];

    function mySayings(){
    //get client choice from options input.
      var x =document.getElementById('nQuotes').value;

      var quote1 = "The capital of " + countries[randomCountry] + " is " + capitals[randomCapital] + "." + "It's currency is " + currency[randomCurrency] + ".";

      var quote2 = Math.floor(Math.random() * myQuotes.length);

      //get the number of quotes client needs from the input.
      var quoteRun= document.getElementById('quotesNeeded').value;
      for (var i = 0; i < quoteRun ; i++) {
        if (x==='a') {
          document.write('here is your quote.'+ ' ' + quote1);
        }
        else if (x==='b') {
        document.write ('Here is your quote. ' + myQuotes[quote2]);
        }
      return false;
      }
    }

    function showQuote(){
      var qPrinter=document.getElementById('demo');
      qPrinter.innerText = mySayings();
    }

    showQuote();

0 个答案:

没有答案