所以我有這個代碼:
<script>
function add7(n) {
let x = n 7;
console.log(x);
}
function lastLetter(theString) {
let x = lastIndexOf(theString);
console.log(x);
})
</script>
<script>
function multiply(a, b) {
let ans = a * b;
console.log(ans);
}
</script>
<script>
function capitalize(word) {
if (word = String) {
toLowerCase(word);
charAt(0).toUpperCase(word);
console.log(word);
} else {
console.log("not a string");
}
}
</script>
我在控制臺中撰寫了 functionName(chosenVariable) 并希望看到一個明確的答案,但 add7 和 lastLetter 只回傳未定義的 ReferenceError (function)。其他 2 個未定義為答案。我知道我是個盲人,但我是不是也有點傻?我查看了代碼并嘗試了不同的更改,但無法使其正常作業。
uj5u.com熱心網友回復:
您的代碼有一些錯誤
- 額外
)在第一個腳本塊的末尾 lastIndexOf未在腳本的任何地方定義word = String只會將String類的值分配給 word 變數(并且總是回傳 true)- 字串是不可變的,因此您無法編輯現有字串,但是可以基于另一個字串創建新字串,因此單獨使用
word.toLowerCase()不會做任何事情,您需要重新分配值
add7(2);
lastLetter("123123");
multiply(2, 3);
capitalize("asd");
function add7(n) {
let x = n 7;
console.log(x);
}
function lastLetter(theString) {
let x = theString[theString.length - 1];
console.log(x);
}
function multiply(a, b) {
let ans = a * b;
console.log(ans);
}
function capitalize(word) {
if (typeof word === "string") {
word = word.toLowerCase();
word = word[0].toUpperCase() word.substring(1);
console.log(word);
} else {
console.log("not a string");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/452154.html
標籤:javascript 功能 安慰
上一篇:為什么這會列印錯誤的東西?
