试图显示警报

时间:2019-03-10 18:07:21

标签: javascript html css

我试图获取一个警报框,以打印出存储在阵列中的温度,具体取决于星期几(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].)");

2 个答案:

答案 0 :(得分:0)

我认为您的主要问题是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].)");

答案 1 :(得分:-1)

除了以前的注释之外,您的代码还以“ ”结尾。在“ <”和“ /”之间有一个空格。