[英]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.