[英]I've been staring at my code for hours and can't find whats wrong with it. - Javascript
问题出在displayInfo函数中。 我运行了程序,一切正常,但没有输出。 输出应该全部放在一个框中,并且应该看起来像我将提供的图片,但是要用上所有的几个月。 输出示例
function main() {
alert("Welcome to the program");
var endProgram = "no";
while (endProgram == "no") {
var notGreenCosts = [12];
var goneGreenCosts = [12];
var savings = [12];
var months = ["January", "February", "March ", "April ", "May ", "June ", "July ", "August", "September", "October", "November", "December"];
getNotGreen(notGreenCosts, months);
getGoneGreen(goneGreenCosts, months);
energySaved(notGreenCosts, goneGreenCosts, savings);
displayInfo(notGreenCosts, goneGreenCosts, savings, months);
endProgram = prompt("Do you want to end the program? Yes or no?");
}
}
function getNotGreen(notGreenCosts, months) {
var counter = 0;
while (counter < 12) {
notGreenCosts[counter] = parseFloat(prompt("Enter NOT GREEN energy costs for " + months[counter]));
counter++;
}
}
function getGoneGreen(goneGreenCosts, months) {
var counter = 0;
while (counter < 12) {
goneGreenCosts[counter] = parseFloat(prompt("Enter GONE GREEN energy costs for " + months[counter]));
counter++;
}
}
function energySaved(notGreenCosts, goneGreenCosts, savings) {
var counter = 0;
while (counter < 12) {
savings[counter] = parseFloat((notGreenCosts[counter] - goneGreenCosts[counter]));
counter++;
}
}
function displayInfo(notGreenCosts, goneGreenCosts, savings, months) {
var counter = 0;
var outputString = "Month \t\t\t not green \t gone green \t savings \n\n";
while (counter < 12) {
outputString += months[counter] + "\t\t\t" + notGreenCosts[counter] + "\t\t\t" + goneGreenCosts[counter] + "\t\t\t" + savings[counter] + "\r\n";
counter++;
}
}
main();
alert("End of program");
实际上,您没有在displayInfo
方法中显示任何内容:
function displayInfo(notGreenCosts, goneGreenCosts, savings, months) {
var counter = 0;
var outputString = "Month \t\t\t not green \t gone green \t savings \n\n";
while (counter < 12) {
outputString += months[counter] + "\t\t\t" + notGreenCosts[counter] + "\t\t\t" + goneGreenCosts[counter] + "\t\t\t" + savings[counter] + "\r\n";
counter++;
}
alert(outputString); // -----> add this line
}
还有另一个问题不会引起任何问题,但是您似乎误解了数组声明。
您的var notGreenCosts = [12];
应该更改为var notGreenCosts = new Array(12);
。 与其他数组声明相同。
检查: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array
我重新编写了您的代码,并提出了这个建议。 我添加了评论,可帮助您了解发生了什么。 我将在您的原始代码中添加摘要,并在后面添加注释。
您只有一个要循环的数组,因此只需要一个while循环。
您要在此处初始化所有具有相同数字值的数组。 不需要。
var notGreenCosts = [12];
var goneGreenCosts = [12];
var savings = [12];
在函数中,您尝试使用循环中传递的计数器从数组中获取值。 如果使用嵌套的while循环来执行该功能。
function getNotGreen(notGreenCosts, months) {
var counter = 0;
while (counter < 12) {
notGreenCosts[counter] = parseFloat(prompt("Enter NOT GREEN energy costs for " + months[counter]));
counter++;
}
}
然后您将获得:
notGreenCosts[0] // returns 12 months[0] // returns January
notGreenCosts[1] // returns undefined months[1] // returns February
notGreenCosts[2] // returns undefined months[2] // returns March
notGreenCosts[3] // returns undefined months[3] // returns April
notGreenCosts[4] // returns undefined months[4] // returns May
可以通过初始化它们来设置这些变量,并将其传递给函数的顶部。 没有将它们设置为数组。
var notGreenCosts = [12];
但是我继续进行初始化,并将它们分配给一行中的返回函数值。
var notGreenCosts = getNotGreen(months, counter);
函数main(){alert(“欢迎使用该程序”);
//inital "End Program" answer
var endProgram = "no";
// Array of Months
var months = ["January", "February", "March ", "April ", "May ", "June ", "July ", "August", "September", "October", "November", "December"];
var counter = 0;
//inital coounter value. Set to ero to match the zero index months array
// while loop will iterate over the months on at a time until it reaches the end of the array. index 11.
while (counter < 11) {
//if the answer to the ed progrm promt question is "no" then run the function and fet the answers.
if (endProgram === "no") {
var notGreenCosts = getNotGreen(months, counter); // run the getNotGreen function and assign the returned value to the notGreenCosts variable.
var goneGreenCosts = getGoneGreen(months, counter); // run the getGoneGreen function and assign the returned value to the goneGreenCosts variable.
var savings = energySaved(notGreenCosts, goneGreenCosts); // run the energySaved function and assign the returned value to the savings variable.
var outPit = displayInfo(notGreenCosts, goneGreenCosts, savings, months, counter); // run the displayInfo function and assign the returned value to the outPit variable.
console.log(outPit); // pint the out come to the console.
// end the program alert.
endProgram = prompt("Do you want to end the program? Yes or no?");
} else {
return // user wants to end the program. Jumps to the end alert.
}
counter++; //add 1 to the counter.
}
}
function getNotGreen(months, counter) {
var answer = parseFloat(prompt("Enter NOT GREEN energy costs for " + months[counter])); // insure that the intered number is a whole number
return answer; // return the anwser.
}
function getGoneGreen(goneGreenCosts, months, counter) {
var answer = parseFloat(prompt("Enter GONE GREEN energy costs for " + months[counter])); // insure that the intered number is a whole number
return answer; // return the anwser.
}
function energySaved(notGreenCosts, goneGreenCosts) {
return parseFloat((notGreenCosts - goneGreenCosts)); // return the calulated number and insure that is a whole number.
}
function displayInfo(notGreenCosts, goneGreenCosts, savings, months, counter) {
var outputString = "Month \t\t\t not green \t gone green \t savings \n\n";
return outputString += months[counter] + "\t\t\t\t" + notGreenCosts + "\t\t\t" + goneGreenCosts + "\t\t\t\t" + savings + "\r\n"; // return the value string.
}
//run the program.
main();
// end the program
alert("End of program");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.