為什么在執行此代碼時所有變數都未定義?
function checkStatus(a, b, c) {
let info = [a, b, c];
let name, age, status;
for (i = 0; i < info.length; i ) {
if (typeof info[i] === "string") {
name = info[i]
} else if (typeof info[i] === Number) {
age = info[i]
} else if (typeof info[i] === Boolean && info[i] === true) {
status = `You Are available for hire `
} else if (typeof info[i] === Boolean && info[i] === false) {
status = `You Are Not available for hire `
};
}
console.log(`Hello ${name} Your Age is ${age} and ${status}`);
}
console.log(checkStatus("yousef", 16, true));
console.log(checkStatus(16, "yousef", true));
console.log(checkStatus(true, 16, "yousef"));
我得到這個輸出:
Hello yousef Your Age is undefined and undefined
Hello yousef Your Age is undefined and undefined
Hello yousef Your Age is undefined and undefined
uj5u.com熱心網友回復:
試試這個,數字和數字/布爾和布爾是兩個不同的東西
function checkStatus(a, b, c) {
let info = [a, b, c];
let name, age, status;
for (i = 0; i < info.length; i ) {
console.log( typeof info[i])
if (typeof info[i] === "string") {
name = info[i]
} else if (typeof info[i] === 'number') {
age = info[i]
} else if (typeof info[i] === 'boolean' && info[i] === true) {
status = `You Are available for hire `
} else if (typeof info[i] === Boolean && info[i] === false) {
status = `You Are Not available for hire `
};
}
console.log(`Hello ${name} Your Age is ${age} and ${status}`);
}
console.log(checkStatus("yousef", 16, true));
console.log(checkStatus(16, "yousef", true));
console.log(checkStatus(true, 16, "yousef"));
uj5u.com熱心網友回復:
運算子typeof
回傳字串而不是物件,這意味著要檢查型別是否為數字,請typeof x === "number"
注意"number"
字串全部小寫,對于"boolean"
.
這是一個修復:
function checkStatus(a, b, c) {
let info = [a, b, c];
let name, age, status;
for (i = 0; i < info.length; i ) {
if (typeof info[i] === "string") {
name = info[i]
} else if (typeof info[i] === "number") {
age = info[i]
} else if (typeof info[i] === "boolean" && info[i] === true) {
status = `You Are available for hire `
} else if (typeof info[i] === "boolean" && info[i] === false) {
status = `You Are Not available for hire `
};
}
console.log(`Hello ${name} Your Age is ${age} and ${status}`);
}
console.log(checkStatus("yousef", 16, true));
console.log(checkStatus(16, "yousef", true));
console.log(checkStatus(true, 16, "yousef"));
uj5u.com熱心網友回復:
Number
是一個函式。您正在將typeof info[i]
(這是一個字串)與Number
哪個函式與typeof info[i] === Number
. 那不是你想要的。
您想與typeof info[i]
('number'
字串)進行比較typeof info[i] === 'number'
(函式)的類似情況Boolean
- 您實際上想要使用'boolean'
字串。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/507995.html
標籤:javascript 循环
下一篇:回傳貓物件中最高的“可愛”評級