I need this program correctly formatted for Javascript. I\'m a begining programm
ID: 3687521 • Letter: I
Question
I need this program correctly formatted for Javascript. I'm a begining programmer and arrays/loops in javascript are really giving me issues please help.
var notGreenCost[6]
var goneGreenCost[6]
var savings[6]
var months[6]
function main(){
var endProgram = “no”
var option = 0
}
function initMonths(){
While (endProgram == “no”){
prompt("What would you like to do? Type:”);
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”);
}
{
return option
}
prompt("Do you want to end the program (enter yes or no)");
While (endProgram<>”no” AND endProgram<>”yes”){
prompt("Please enter a value of yes or no");
}
{
return endProgram
}
End While
End While
}
function writeToFile(){
//Add statements to write the month and savings to the file
}
function readFromFile(){
//Add statements to read the month and savings from the file and display the info
}
function initMonths(){
months = “January”, “February”, “March”, “April”, “May”, “June”
}
function getNotGreen(){
Integer counter = 0
While (counter < 6){
alert("Enter NOT GREEN energy costs for”), months[counter]")
}
return notGreenCost[counter];
}
Set counter = counter + 1
}
function getGoneGreen(){
counter = 0;
While (counter < 6){
prompt("Enter GONE GREEN energy costs for”), months[counter]);
}
return goneGreenCost[counter];
Set counter = counter + 1
}
function energySaved(){
counter = 0;
While (counter < 6){
savings[counter] = notGreenCost[counter] – goneGreenCost[counter]
counter = counter + 1
}
function displayInfo(){
alert("SAVINGS NOT GREEN GONE GREEN MONTH")
alert("_________________________________________________")
alert(" ")
Integer counter = 0
While (counter < 6){
alert( "$" + savings[counter] + "$" + notGreenCost[counter] + "$" + goneGreenCost[counter] + "" + months[counter])
counter = counter + 1
}
Explanation / Answer
following chnages are needed in the code:
function initMonths(){
While (endProgram == “no”){
var choice = prompt ("What would you like to do Type”, "1);
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
<html>
<body>
<script language="javascript" type="text/javascript">
var notGreenCost[6]
var goneGreenCost[6]
var savings[6]
var months[6]
var ipFile = new File("E:/Files/FileIP.txt");
var opFile = new File("E:/Files/FileOP.txt");
function main(){
var endProgram = “no”
var option = 0
} // end function main
function initMonths(){
While (endProgram == “no”){
option = prompt("What would you like to do Type”, "1);
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”);
}
{
return option
}
prompt("Do you want to end the program (enter yes or no)");
While (endProgram<>”no” AND endProgram<>”yes”){
prompt("Please enter a value of yes or no");
}
{
return endProgram
}
End While
End While
} // end function init months
function writeToFile(){
//Add statements to write the month and savings to the file
opFile.open("w+"); // open the output file in write mode
opFile.writeln(month);
opFile.writeln(savings);
}
function readFromFile(){
//Add statements to read the month and savings
// from the file and display the info
//var savings[6]
//var months[6]
ipFile.open("r"); // open the input file in read mode
while (!ipFile.eof) { // read them from the file
month = ipFile.readln();
savings = ipFile.readln();
} // end while
ipFile.close();
alert(month); // display them
alert(savings);
} // end read file
function initMonths(){
months = “January”, “February”, “March”, “April”, “May”, “June”
}
function getNotGreen(){
Integer counter = 0
While (counter < 6){
alert("Enter NOT GREEN energy costs for”), months[counter]")
}
return notGreenCost[counter];
}
Set counter = counter + 1
}
function getGoneGreen(){
counter = 0;
While (counter < 6){
prompt("Enter GONE GREEN energy costs for”), months[counter]);
}
return goneGreenCost[counter];
Set counter = counter + 1
}
function energySaved(){
counter = 0;
While (counter < 6){
savings[counter] = notGreenCost[counter] – goneGreenCost[counter]
counter = counter + 1
} // end while
function displayInfo(){
alert("SAVINGS NOT GREEN GONE GREEN MONTH")
alert("_________________________________________________")
alert(" ")
Integer counter = 0
While (counter < 6){
alert( "$" + savings[counter] + "$" + notGreenCost[counter] + "$" + goneGreenCost[counter] + "" + months[counter])
counter = counter + 1
} // end while
</script>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.