第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

用 JavaScript 為學(xué)校作業(yè)創(chuàng)建一個(gè)字母猜謎游戲

用 JavaScript 為學(xué)校作業(yè)創(chuàng)建一個(gè)字母猜謎游戲

函數(shù)式編程 2022-05-26 17:11:38
所以我一直堅(jiān)持這個(gè)練習(xí)太久了,不知道我做錯(cuò)了什么。我在處理數(shù)組和合并循環(huán)時(shí)遇到了困難。任務(wù)是將 10 個(gè)字母放入一個(gè)數(shù)組中,讓用戶至少猜對一個(gè)字母。3 次嘗試后告訴用戶他們已經(jīng)丟失并終止代碼。<!DOCTYPE html><html><head>  <title>Exercise 5</title>  <link rel="stylesheet" type="text/css" href="css/styles5.css"/></head><body>  <!--E X C E R S I S E 5 !-->  <section id="allContent">    <div id="container">      <div class="bar1"></div><div id="bar2"></div>      <h1 id="excersize">E x c e r c i c e &ensp;5</h1>      <div id="titleBox">        <p id="titlePRG">Enter five words</p>      </div>      <input type="text"   id="textField">      <form>        <input type="button" value="Enter" class="button" onclick="getValue()">         </form>      <div id="valueReturned">        <p id="returnText"><p>        </div>        <a href="index6.html"><h3> Next Exercise </h3></a>      </div>      <img src ="blank-laptop-png-transparent-pictures-free-icons-graphic-transparent-library-laptop-png-4042_3027.png" alt="laptopGraphic">  </section>    <!--E N D   O F   E X C E R c I c E 5 !-->            <!-- S C R I P T I N G !-->            <script>        function getValue(){          var input = document.getElementById("textField").value;          var letters  =  ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"] ;          var a = letters.indexOf(input);          for(var attempts = 0; attempts < 3; attempts++){            if(input == ""){            document.getElementById("valueReturned").innerHTML = "No input!";            }else if( a >= 0){              document.getElementById("valueReturned").innerHTML = "You guessed right!";             break;            }           }// end of function   </script>   </body>   </html> 因此,如果用戶猜對了,我希望代碼停止運(yùn)行或停止循環(huán)。如果在第一次嘗試時(shí)猜錯(cuò)了,那么它應(yīng)該顯示一條消息“還剩兩次嘗試!” 如果第二次嘗試時(shí)猜測錯(cuò)誤,則應(yīng)顯示消息“還剩一次嘗試!” 等等...我現(xiàn)在沒有用 else 語句關(guān)閉我的條件,不確定是否需要。我的 For 循環(huán)設(shè)置不正確嗎?我的 if,else if 條件不正確嗎?我非常需要你們的幫助!
查看完整描述

2 回答

?
慕田峪9158850

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超7個(gè)贊

問題是您要為每次嘗試迭代 3 次嘗試。


對于所有嘗試,您需要全局迭代。


換句話說,跟蹤函數(shù)之外的嘗試,否則每次調(diào)用函數(shù)時(shí),您都會將嘗試重置為 3 - 它永遠(yuǎn)不會失敗。


這是我對您的代碼的重構(gòu),它還修復(fù)了一些其他問題并對其進(jìn)行了優(yōu)化。


(() => {

    let attempts = 3,

        input_el = document.getElementById('textField'),

        result_el = document.getElementById('valueReturned');

    window.getValue = () => {

        let input = input_el.value,

        letters  =  ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],

        correct = letters.includes(input),

        msg;


        if (!attempts)

            msg = 'No attempts left!';

        else if (!input)

            msg = "No input!";

        else if(!correct) {

            attempts--;

            switch (attempts) {

                case 2:

                    msg = 'Two tries left!'

                    break;

                case 1:

                    msg = 'One more try!';

                    break;

                case 0:

                    msg = 'That was your last try! Get lost!';

                    break;

            }

        } else

            msg = 'You guessed right!';


        result_el.innerHTML = msg;


    }

})();

一些注意事項(xiàng):

  • 指示性地命名你的變量

  • 將您的 JS 與您的 HTML 分開 - 將其放入專用.js文件中。

  • let這些var天使用

  • onclick考慮集中式事件處理,而不是通過屬性內(nèi)聯(lián) JS 。這也意味著我們不必聲明一個(gè)全局函數(shù)window供您onclick參考。

  • 我們將整個(gè)內(nèi)容包裝在 IIFE(立即調(diào)用函數(shù)表達(dá)式)中,以防止污染全局命名空間。

  • 想想每次事件觸發(fā)時(shí)不需要復(fù)制的內(nèi)容。我們不需要每次都從 DOM 中獲取對相同元素的引用——讓我們在函數(shù)之外進(jìn)行。

  • array.includes(val)相當(dāng)于array.indexOf(val) !== -1

  • 我還讓用戶無法進(jìn)行超過 3 次嘗試。


查看完整回答
反對 回復(fù) 2022-05-26
?
阿晨1998

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊

讓我們看看你的邏輯


<script>

        function getValue(){

          var input = document.getElementById("textField").value;

          var letters  =  ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"] ;

          var a = letters.indexOf(input);

在這一點(diǎn)上,您打開了一個(gè)腳本標(biāo)簽并聲明了一個(gè)函數(shù)getValue。然后你用 id 從 html 元素中獲取輸入,用 10 個(gè)字母"textField"初始化一個(gè)數(shù)組,然后搜索,返回它的索引。但是,這會導(dǎo)致輸入僅被讀取一次。然后它將執(zhí)行您的 for 循環(huán)進(jìn)行 3 次迭代,從而產(chǎn)生潛在的有害錯(cuò)誤。letterslettersinput


這樣想,


您的功能需要在某個(gè)地方啟動。通過將其嵌入到 html 中,它將出現(xiàn)onclick()在輸入下方的按鈕中。這意味著每次調(diào)用該函數(shù)時(shí),都會讀取一次輸入,并且循環(huán)在同一個(gè)輸入上運(yùn)行 3 次。我將首先在您的getValue函數(shù)之外創(chuàng)建一個(gè)變量——我們將調(diào)用它——這attempts將允許我們在每次運(yùn)行函數(shù)時(shí)更新變量。然后擺脫 for 循環(huán),并使用條件分支,例如


var attempts = 0;

function getValue(){

  var input = document.getElementById("textField").value;

  var letters  =  ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"] ;

  var a = letters.indexOf(input);

  if (attempts < 3) {

    // check if input is right

    if (input != "" && a != -1) {

       alert("success");

    }

    attempts += 1;

  }

// no else statement needed to terminate the program

// simply don't handle occurrences of attempts greater than  3

}

請?jiān)u論您不確定的任何內(nèi)容


查看完整回答
反對 回復(fù) 2022-05-26
  • 2 回答
  • 0 關(guān)注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號