2016-04-10 2 views
1

BEARBEITEN Ich postete das ursprünglich mit meiner Version der J.S.Sie ist aber so weit entfernt, dass niemand helfen kann, also fange ich neu an. Hier ist der Pseudocode, den ich gemacht habe, der in ein Javascript-Programm übersetzt werden muss. Jede Hilfe wird geschätzt!JavaScript und Array's

Ich bin ein Anfänger Programmierer Ich verstehe, dass dieser Code mehrere Fehler haben wird, deshalb bin ich hier. Arrays und Schleifen haben mir beim Versuch, sie zu lernen und insbesondere mit JavaScript zu formatieren, viel Mühe bereitet. Die Dinge, die ich kenne, sind falsch oder müssen immer noch auskommentiert werden, ich brauche sie immer noch, ich weiß auch, dass ich nichts übergebe, ich kann einfach nicht meinen Kopf darum drehen, wie man sie dorthin bringt. Ich bin auch nicht sicher, ob während ich Input sammle, benutze alter und prompte richtig. In der Anzeigefunktion ist der Abstand für den Zeitpunkt der Anzeige erforderlich. Korrekturen und Erklärungen werden sehr geschätzt.

Module main() 
//Declare local variables 
Declare endProgram = “no” 
While endProgram == “no” 
    Declare Real notGreenCost[12] 
    Declare Real goneGreenCost[12] 
    Declare Real savings[12] 
Declare String months[12] = “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” 

    //function calls 
    getNotGreen(notGreenCost, months) 
    getGoneGreen(goneGreenCost, months) 
    energySaved(notGreenCost, goneGreenCosts, savings) 
    displayInfo(notGreenCost, goneGreenCosts, savings, months) 

    Display “Do you want to end the program? Yes or no” 
    Input endProgram 
End While 
End Module 

Module getNotGreen(Real notGreenCost[], String months[]) 
Set counter = 0 
While counter < 12 
    Display “Enter NOT GREEN energy costs for”, months[counter] 
    Input notGreenCosts[counter] 
    Set counter = counter + 1 
End While 
End Module 

Module getGoneGreen(Real goneGreenCost[], String months[]) 
Set counter = 0 
While counter < 12 
    Display “Enter GONE GREEN energy costs for”, months[counter] 
    Input goneGreenCosts[counter] 
    Set counter = counter + 1 
End While 
End Module 

Module energySaved(Real notGreenCost[], Real goneGreenCost[], Real savings[]) 
Set counter = 0 
While counter < 12 
    Set savings[counter] = notGreenCost[counter] – goneGreenCost[counter] 
    Set counter = counter + 1 
End While 
End Module 
Module displayInfo(Real notGreenCost[], Real goneGreenCost[], Real savings[], String months[]) 
Set counter = 0 
While counter < 12 
    Display “Information for”, months[counter] 
    Display “Savings $”, savings[counter] 
    Display “Not Green Costs $”, notGreenCost[counter] 
    Display “Gone Green Costs $”, goneGreenCost[counter] 
End While 
End Module 
+0

Sind Sie NodeJS, oder ist dies im Browser funktionieren soll? – JazzCat

+0

@JazzCat Ich glaube Browser. Ich habe es nur an einen Container angeschlossen, um zu laufen. –

+2

Es ist so viel falsch mit Ihrem Code. Ich würde vorschlagen, dass Sie versuchen würden, https://www.freecodecamp.com/ – JazzCat

Antwort

0

Ein paar Anmerkungen:

  • Derzeit erstellt das Programm ein paar Variablen und Funktionen, die scheinen nicht
  • Die meisten der Änderungen sind unten zu interagieren nicht optimal - es Teile gibt, die könnte durch viel einfachere Mittel (dh Zähler ++) - Aber das ist für Sie zu lernen = P
  • Ich machte eine Reihe von Annahmen, was Sie wollten das Programm tun, könnten sie falsch sein, könnten sie Recht

    var notGreenCost = []; //Array lengths don't need to be specified 
    var goneGreenCost = []; 
    var savings = []; 
    var months = ["January", "Feburary", "March", "April", "May", "June"]; 
    
    //A boolean value (true | false) would suit this better as opposed to "yes"/ "no" 
    var endProgram = false; 
    var option = 0; 
    
    /* You dont need main functions in javascript 
    * migrated everything to be global :/ 
    * Delete: 
        function main(){ 
         // Move this (made it global): var endProgram = "no"; 
        } 
    */ 
    // I don't think this is meant to be initMonths.. 
    // Maybe something like getOptions? 
    function /*initMonths*/getOptions(){ 
        while (endProgram == false){ //lowercase while 
         //Because prompt would block everything else until it gets input 
         //we probably want to move the prompt to be after the alerts 
         alert("options:"); //Clarity 
         alert("1 to enter data"); 
         alert("2 to display data"); 
         alert("3 to write data to a file"); 
         alert("4 to read data from a file"); 
         //Alter global "option" to take the value of the prompt 
         option = prompt("What would you like to do? Type:"); 
         //} //I assume you want the rest of the code in this while loop - otherwise it will loop forever 
    
         // Delete this bracket (its unmatched): { 
         // Delete return statement as it will stop the function return option; 
         // Delete this bracket (its unmatched): } 
    
         //Create a variable to take the value of prompt (this should be outside the while loop) but it seem clearer for explanation purposes to be here 
         var toEnd; 
         toEnd = prompt("Do you want to end the program (enter yes or no)"); 
         // Javascript uses != for "not equal to" and && for "AND" 
         while (toEnd != "no" && toEnd != "yes") { 
          toEnd = prompt("Please enter a value of yes or no"); 
         } 
    
         //I think you want to assign the value of toEnd to endProgram 
         // Note the the below is not the only/best way to do it 
         if(toEnd == "no") { 
          endProgram = false; 
         } else if(toEnd == "yes") { 
          endProgram = true; 
         } 
    
         // While use brackets not End s 
         //  End While 
         // End While 
        }//End while loop here 
    } 
    

Javascript im Browser sein können Dateien nicht ändern - writeToFile haben Readfromfile alle

Ich glaube, entfernt man Monate global sein wollen, wenn es dann ist initMonths unnötige

getNotGreen:

function getNotGreen(){ 
    //You don't need to specify types in Javascript 
    /*Integer*/ var counter = 0 
    while (counter < 6){ //lowercase while 
     //I'm assuming you want to combine the values of "Enter NOT GREEN energy costs for" and months[counter] - This is done by using the + sign 
     //Im also assuming you want to read the value into notGreenCost 
     //.push adds a value to a array 
     notGreenCost.push(prompt("Enter NOT GREEN energy costs for" + months[counter])) 

     //Returning here makes the rest of the function redundant 
     //} 
     //return notGreenCost[counter]; 
     //} 
     //Javascript does not use Set 
     // Note that below is not the only/best way to do it 
     /*Set*/ counter = counter + 1 
    } //End the while loop here 
} 

getGoneGreen:

function getGoneGreen(){ 
    //Counter should probably be local (not global) - use var 
    var counter = 0; 
    while (counter < 6){//lowercase while 
     //I'm assuming you want to combine the values of "Enter NOT GREEN energy costs for" and months[counter] - This is done by using the + sign 
     //Im also assuming you want to read the value into notGreenCost 
     //.push adds a value to a array 
    goneGreenCost.push(prompt("Enter GONE GREEN energy costs for" + months[counter])); 
     //See above (getNotGreen) 
     //} 
     //return goneGreenCost[counter]; 
     /*Set*/ counter = counter + 1; 
    }//End while loop here 
} 

energySaved:

function energySaved(){ 
    //Counter should probably be local (not global) - use var 
    var counter = 0; 
    while (counter < 6){//lowercase while 
     savings[counter] = notGreenCost[counter] - goneGreenCost[counter] 
     counter = counter + 1; 
    } 
} //I assume you want to end energySaved here? 

displayInfo:

function displayInfo(){ 
    //Alert produced individual boxes, i assume you want the following in a single window? 
    // "\n" is a line break 
    alert("SAVINGS  NOT GREEN GONE GREEN  MONTH\n"+ 
      "_________________________________________________\n"); 
    //Counter should probably be local (not global) - use var 
    var counter = 0; 
    while (counter < 6){//lowercase while 
     alert("$" + savings[counter] + "$" + notGreenCost[counter] + "$" + goneGreenCost[counter] + "" + months[counter]); 
     counter = counter + 1; 
    } 
} //I assume you want to end displayInfo here? 
+0

Du bist unglaublich! Wie lange hast du das gemacht? Versuchen Sie wirklich, Wissen wie Ihres zu haben. –