2017-08-14 98 views
1

我是一名初學者程序員,我想完成這項工作,但事實並非如此。你能幫我解釋一下有什麼問題嗎?例如,如果我輸入20以下,我希望它告訴我要花多長時間才能走到我選擇的任何行星。其中大部分是弄清楚如何讓代碼存儲用戶選擇的任何選項,然後將其應用於年齡並使用數據來計算它。謝謝!如何製作一種使用html和javascript的計算器?

<legend>basic info</legend> 
    <p> What is your destination? 
     <select onchange = "selectDestination(this.value)"> 
     <option value="mercury">Mercury</option> 
     <option value="venus">Venus</option> 
     <option value="mars">Mars</option> 
     <option value="jupiter">Jupiter</option> 
     <option value="saturn">Saturn</option> 
     <option value="uranus">Uranus</option> 
     <option value="neptune">Neptune</option> 
     <option value="pluto">Pluto</option> 
     </select> 
     <script type="text/javascript"> 
      var planets = new Array(); 
     planets['mercury'] = 48000000; 
     planets['venus']=25000000 
     planets['mars']=25000000 
     planets['jupiter']=33900000 
     planets['saturn']=365000000 
     planets['uranus']=1200000000 
     planets['neptune']=2600000000 
     planets['pluto']=2800000000 

     function selectDestination(selectedValue) { 
     window.alert(selectedValue + " distance=" + planets[selectedValue]); 
     }   
     </script> 
    </p> 
    <p> 
    <label>how old are you?</label> 
    <form id="form" onsubmit="return false;"> 
     <input type="number" id="userInput"> 
     <input type="submit" onclick="age()"> 
    </form> 
    <script type="text/javascript"> 
     function age() 
     { 
     var input = document.getElementById("userInput"); 
     alert(input); 
     } 
     if(age<20); 
     { 
     alert("YOU ARE UNDER 20"); 
     var runSpeed=6.3; 
     var walkSpeed=2.1 
     var water = 1.5; 
     var calories = 2500; 
     var runTime = planets[selectedValue]/runSpeed; 
     var runDays = runTime/24 
     document.write("it will take "+runTime+" hours and "+runDays+" days to make it to"+selectedValue); 
     } 
     if(21<age>45) { 
     alert("YOU ARE AT YOUR PHYSICAL PEAK"); 
     var runSpeed=8.3; 
     var walkSpeed=3.1; 
     var water = 2; 
     var calories = 3000; 
     var runTime = planets[selectedValue]/runSpeed; 
     var runDays = runTime/24; 
     document.write("it will take "+runTime"hours and"+runDays+"days to make it to"+selectedValue); 
     } 
     if(age<45){ 
     alert("YOU ARE PROBABLY TOO OLD TO TAKE THIS TRIP"); 
     var runSpeed=5.3; 
     var walkSpeed=1.1; 
     var water=1.5; 
     var calories=3000; 
     var runTime = planets[selectedValue]/runSpeed; 
     var rundays = planets[selectedValue]/24; 
     document.write("it will take "+runTime"hours and"+runDays+"days to make it to"+selectedValue); 
     } 
    </script> 
    </p> 
+2

您的代碼有許多問題。首先,你缺少一個關閉'''括號;調試你的代碼來找到它。其次,你的年齡計算'if(21 < age > 45)'將不起作用 - 你需要將它分成兩個不同的條件,或者寫'if(age> 21 && age <45)'。你也可能爲你的行星尋找一個**關聯數組**或** **對象**。 –

回答

3

此處的代碼無效。

 var planets = new Array(); 
    planets['mercury'] = 48000000; 
    planets['venus']=25000000 
    planets['mars']=25000000 
    planets['jupiter']=33900000 
    planets['saturn']=365000000 
    planets['uranus']=1200000000 
    planets['neptune']=2600000000 
    planets['pluto']=2800000000 

你聲明的是一個數組,你想推入的是對象。

你最好要行星看起來像什麼是var planets = [ {planet: 'mercury', distance: 48000000}, {planet: 'venus', distance: 25000000 }, ... ]

現在,什麼也沒發生,因爲你的語法正在尋找與mercuryvenus一個鍵的對象,並沒有找到任何東西。

你應該做的下一件事是能夠console.log()無論用戶輸入的年齡。確保你的功能age()接受參數,所以它看起來更像age(val)

一旦你console.log()記錄用戶輸入的年齡,你應該非常接近。所有你需要做的是抓住用戶選擇的星球,你可以通過循環訪問你的planets陣列,並匹配用戶選擇的數組和內容。像planets.filter(planet => planet.indexOf(planetUserSelected) !== -1)這樣的東西可以讓你找回你正在尋找的物品,並且你可以從那裏獲得價值並且從console.log()

相關問題