所以伙計們,我想制作一個特定的字符計數器,fg in word Specific 我們有 2 個“i”。我哪里錯了?
function countChar(word, char) {
var count = 0
for (let i = 0; i <= word.lenght; i ) {
if (word[i] === `${char}`) {
count = 1
}
}
return count;
}
console.log(countChar("BBC","B"));
//0
//undefined
uj5u.com熱心網友回復:
您好,歡迎來到stackoverflow!
您的代碼中有兩個問題:
- 錯字 word.length 而不是 word.length
- 比較中的 word[i] 而不是 string[i]
因此,您的代碼在您使用時有效
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
function countChar(word, char) {
var count = 0
for (let i = 0; i <= word.length; i ) {
if (word[i] === `${char}`) {
count = 1
}
}
return count;
}
alert (countChar("BBC","B"));
</script>
</head>
<body>
</body>
</html>
請使用除錯器,就像將來每個現代瀏覽器中包含的除錯器一樣;-)。
uj5u.com熱心網友回復:
for 回圈中有輸入錯誤。寫長度而不是長度。
這是作業代碼
function countChar(word, char) {
var count = 0
for (let i = 0; i <= word.length; i ) {
if (word[i] === `${char}`) {
count = 1
}
}
return count;
}
console.log(countChar("BBC","B"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/443134.html
標籤:javascript 排序
