[英]I've been staring at my code for hours and can't find whats wrong with it. - Javascript
The problem is in the displayInfo function. 问题出在displayInfo函数中。 I ran the program and everything works but it doesn't give me the output.
我运行了程序,一切正常,但没有输出。 The output should all be in one box and should look something the picture i will provide but for all the months.
输出应该全部放在一个框中,并且应该看起来像我将提供的图片,但是要用上所有的几个月。 output example
输出示例
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");
actually you are are not displaying anything in your displayInfo
method: 实际上,您没有在
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
}
There is another issue which is not causing any problem, but you seem to misunderstood array declaration. 还有另一个问题不会引起任何问题,但是您似乎误解了数组声明。
Your var notGreenCosts = [12];
您的var
notGreenCosts = [12];
should be changed to var notGreenCosts = new Array(12);
应该更改为var
notGreenCosts = new Array(12);
. 。 Same for other array declarations.
与其他数组声明相同。
Check: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 检查: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array
I reworked your code and came up with this. 我重新编写了您的代码,并提出了这个建议。 I added comments that will help you understand what is going on.
我添加了评论,可帮助您了解发生了什么。 I will add snippets from your original code with comments latter.
我将在您的原始代码中添加摘要,并在后面添加注释。
You only have one array that you are looping over so you only need one while loop. 您只有一个要循环的数组,因此只需要一个while循环。
You are initializing arrays here all with the same number value. 您要在此处初始化所有具有相同数字值的数组。 It is not needed.
不需要。
var notGreenCosts = [12];
var goneGreenCosts = [12];
var savings = [12];
In your functions you try to grab the value from your array using the counter that is being passed in the loop. 在函数中,您尝试使用循环中传递的计数器从数组中获取值。 If you do the function with the nested while loop.
如果使用嵌套的while循环来执行该功能。
function getNotGreen(notGreenCosts, months) {
var counter = 0;
while (counter < 12) {
notGreenCosts[counter] = parseFloat(prompt("Enter NOT GREEN energy costs for " + months[counter]));
counter++;
}
}
Then you will get: 然后您将获得:
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
These variables could be set and passed to the functions at the top the way you have them by just initializing them. 可以通过初始化它们来设置这些变量,并将其传递给函数的顶部。 Not setting them to arrays.
没有将它们设置为数组。
var notGreenCosts = [12];
But i went ahead and initializing and assigned them to the returned function value in one line. 但是我继续进行初始化,并将它们分配给一行中的返回函数值。
var notGreenCosts = getNotGreen(months, counter);
function main() { alert("Welcome to the program"); 函数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.