我正在尝试使用一个警告框来打印存储在阵列中的温度,具体取决于星期几(0-6),但没有发生任何事情。
我哪里错了?
码:
var highTemps = ["32", "41", "36", "29", "39", "37", "40"];
var lowTemps = ["18", "24", "20", "27", "30", "31", "27"];
var d = new Date();
var n = d.getDay();
if(n == 0){
alert("Expect a high of " highTemps[0] " and a low of " lowTemps[0] " today.");
}else if(n == 1){
alert ("Expect a high of" highTemps[1] "and a low of" lowTemps[1] "today.");
}else if(n == 2){
alert("Expect a high of" highTemps[2] "and a low of" lowTemps[2] "today.");
}else if(n == 3){
alert("Expect a high of" highTemps[3] "and a low of" lowTemps[3] "today.");
}else if(n == 4){
alert("Expect a high of" highTemps[4] "and a low of" lowTemps[4] "today.");
}else if(n == 5){
alert("Expect a high of" highTemps[5] "and a low of" lowTemps[5] "today.");
}else if(n == 6){
alert("Expect a high of" highTemps[6] "and a low of" lowTemps[6] "today.");
}else {
alert("Who knows what the temperature is?!");
};
console.log("The average temperature for this week is [average].");
console.log("The warmest day of the week will have a high temperature of [hTemperature]");
console.log("The lowest low temperature of the week will be [lTemperature].)");
您的问题是您需要使用+
运算符将消息的静态部分与动态部分连接起来:
var highTemps = ["32", "41", "36", "29", "39", "37", "40"];
var lowTemps = ["18", "24", "20", "27", "30", "31", "27"];
var d = new Date();
var n = d.getDay();
if(n == 0){
alert("Expect a high of " + highTemps[0] + " and a low of " + lowTemps[0] + " today.");
}else if(n == 1){
alert ("Expect a high of" + highTemps[1] + "and a low of" + lowTemps[1] + "today.");
}else if(n == 2){
alert("Expect a high of" + highTemps[2] + "and a low of" + lowTemps[2] + "today.");
}else if(n == 3){
alert("Expect a high of" + highTemps[3] + "and a low of" + lowTemps[3] + "today.");
}else if(n == 4){
alert("Expect a high of" + highTemps[4] + "and a low of" + lowTemps[4] + "today.");
}else if(n == 5){
alert("Expect a high of" + highTemps[5] + "and a low of" + lowTemps[5] + "today.");
}else if(n == 6){
alert("Expect a high of" + highTemps[6] + "and a low of" + lowTemps[6] + "today.");
}else {
alert("Who knows what the temperature is?!");
};
console.log("The average temperature for this week is [average].");
console.log("The warmest day of the week will have a high temperature of [hTemperature]");
console.log("The lowest low temperature of the week will be [lTemperature].)");
现在,说了这么多,我们可以通过使用n
变量作为数组的索引来简化你的代码,并摆脱所有if/then
。
另外,使用if / then的类型你有一个switch
语句可以更简洁:
var highTemps = ["32", "41", "36", "29", "39", "37", "40"];
var lowTemps = ["18", "24", "20", "27", "30", "31", "27"];
var d = new Date();
var n = d.getDay();
// If the array contains an index of the current day number
if(highTemps.indexOf(n) > -1){
// Just extract that element from the arrays and use it.
alert("Expect a high of " + highTemps[n] + " and a low of " + lowTemps[n] + " today.");
} else {
// Otherwise, show the default message
alert("Who knows what the temperature is?!");
}
console.log("The average temperature for this week is [average].");
console.log("The warmest day of the week will have a high temperature of [hTemperature]");
console.log("The lowest low temperature of the week will be [lTemperature].)");
警报功能有问题。字符串消息应使用'+'附加数组值。
<script>
var highTemps = ["32", "41", "36", "29", "39", "37", "40"];
var lowTemps = ["18", "24", "20", "27", "30", "31", "27"];
var d = new Date();
var n = d.getDay();
if(n == 0){
alert("Expect a high of " + highTemps[0] +" and a low of " +lowTemps[0] + " today.");
}else if(n == 1){
alert ("Expect a high of" + highTemps[1] + "and a low of" +lowTemps[1] + "today.");
}else if(n == 2){
alert("Expect a high of" + highTemps[2] + "and a low of" + lowTemps[2] + "today.");
}else if(n == 3){
alert("Expect a high of" + highTemps[3] + "and a low of" + lowTemps[3] + "today.");
}else if(n == 4){
alert("Expect a high of" + highTemps[4] + "and a low of" +lowTemps[4]+ "today.");
}else if(n == 5){
alert("Expect a high of" + highTemps[5] + "and a low of" + lowTemps[5] + "today.");
}else if(n == 6){
alert("Expect a high of" + highTemps[6] + "and a low of" + lowTemps[6] + "today.");
}else {
alert("Who knows what the temperature is?!");
};
console.log("The average temperature for this week is [average].");
console.log("The warmest day of the week will have a high temperature of [hTemperature]");
console.log("The lowest low temperature of the week will be [lTemperature].)");
我认为你的主要问题是window
缺少window.alert
,你也可以使用ES6语法来避免使用+
:
const highTemps = ["32", "41", "36", "29", "39", "37", "40"];
const lowTemps = ["18", "24", "20", "27", "30", "31", "27"];
const d = new Date();
const n = d.getDay();
if(n === 0) {
window.alert(`Expect a high of ${highTemps[0]} and a low of ${lowTemps[0]} today.`);
} else if(n === 1) {
window.alert (`Expect a high of ${highTemps[1]} and a low of ${lowTemps[1]} today.`);
} else if(n === 2) {
window.alert (`Expect a high of ${highTemps[2]} and a low of ${lowTemps[2]} today.`);
} else if(n === 3) {
window.alert (`Expect a high of ${highTemps[3]} and a low of ${lowTemps[3]} today.`);
} else if(n === 4) {
window.alert (`Expect a high of ${highTemps[4]} and a low of ${lowTemps[4]} today.`);
} else if(n === 5) {
window.alert (`Expect a high of ${highTemps[5]} and a low of ${lowTemps[5]} today.`);
} else if(n === 6) {
window.alert (`Expect a high of ${highTemps[6]} and a low of ${lowTemps[6]} today.`);
} else {
alert("Who knows what the temperature is?!");
};
console.log("The average temperature for this week is [average].");
console.log("The warmest day of the week will have a high temperature of [hTemperature]");
console.log("The lowest low temperature of the week will be [lTemperature].)");
或者你甚至可以:
const highTemps = ["32", "41", "36", "29", "39", "37", "40"];
const lowTemps = ["18", "24", "20", "27", "30", "31", "27"];
const d = new Date();
const n = d.getDay();
for (let i = 0; i < highTemps.length; i++) {
if (n === i) {
window.alert(`Expect a high of ${highTemps[i]} and a low of ${lowTemps[i]} today.`);
}
if (n >= 7) {
window.alert("Who knows what the temperature is?!");
}
}
console.log("The average temperature for this week is [average].");
console.log("The warmest day of the week will have a high temperature of [hTemperature]");
console.log("The lowest low temperature of the week will be [lTemperature].)");
除了以前的注释,您的代码以“</ script>”结尾。 “<”和“/”之间有一个空格。