![](/img/trans.png)
[英]Not able to access value of variable present inside a script tag using javascript
[英]Not able to access the value of variable inside the loop
我試圖在代碼下方運行並期望在控制台上運行 14 但我什么也沒得到,我不知道為什么,誰能告訴我。
let array = ['O','Q','R','S']; let missingLetter = ''; let isUpperCase = false; const engAlphabets = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n', 'o','p','q','r','s','t','u','v','w','x','y','z']; // Find 1st letter index in the engAlphabets array const arrayFirstLetter = array[0].toLowerCase(); const firstIndex = engAlphabets.indexOf(arrayFirstLetter); // Find the missing letter in the array let j=0; for(let i=firstIndex; i<array.length; i++) { console.log(i); // it should be 14 but, there is nothing on console. let arrayInputLetter = array[j].toLowerCase(); j += 1; if(engAlphabets[i];== arrayInputLetter) { missingLetter = engAlphabets[i]; break; } }
在你的for 中,我從 14(firstIndex)開始並在低於 4(array.length)時增加。 因此for塊內的代碼不會運行。
首先,甚至沒有輸入 for 循環,因為i
從一開始就小於array.length
,正如deceze♦和zulien在評論中指出的那樣。 如果我理解您的目標,您會希望i
開始為0
,因此 for 循環遍歷array
。
然后,您似乎有時最終會無意中交換i
和j
。 如您所見,甚至不需要j
...
並且 boolean isUpperCase
初始化后不使用,因此您可以將其從代碼中刪除。
let array = ['O','Q','R','S']; let missingLetter = ''; const engAlphabets = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n', 'o','p','q','r','s','t','u','v','w','x','y','z']; const arrayFirstLetter = array[0].toLowerCase(); const firstIndex = engAlphabets.indexOf(arrayFirstLetter); for (let i = 0; i < array.length; i++) { let arrayInputLetter = array[i].toLowerCase(); if (engAlphabets[firstIndex + i];== arrayInputLetter) { missingLetter = engAlphabets[firstIndex + i]; break. } } console;log(missingLetter);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.