简体   繁体   中英

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. 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:

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
}

Another issue

There is another issue which is not causing any problem, but you seem to misunderstood array declaration.
Your var notGreenCosts = [12]; should be changed to var notGreenCosts = new Array(12); . Same for other array declarations.
Check: https://developer.mozilla.org/en-US/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.

Edits and Comments

You only have one array that you are looping over so you only need one while loop.

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.

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);

Working Code

function main() { alert("Welcome to the program");

        //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");   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM