You are viewing our Forum Archives. To view or take place in current topics click here.
Whats wrong with my coding java
Posted:

Whats wrong with my coding javaPosted:

pompe2016
  • New Member
Status: Offline
Joined: Nov 22, 201310Year Member
Posts: 46
Reputation Power: 1
Status: Offline
Joined: Nov 22, 201310Year Member
Posts: 46
Reputation Power: 1
I got this feedback from my teacher about my code but i dont know how to fix it

There is an issue with the area, it might be because you do not calculate it until after all the conditionals. Also it should not output with a .
There is also an issue with the undercoat cost, this might be because you have 2 variables with the same name.





var help = prompt("Do you need any help?");
if (help ==="yes"){
myFunction();
}

else if(help ==="no"){

}

myFunction = function(){
alert("First enter the height of your room into the box");
alert("Enter the first length of your room");
alert("Enter the second length of your room");
alert("Enter the third length of your room");
alert("Enter the fourth length of your room");
alert("Then type in what paint you want your room to be");
alert("Choose is you want undercoat by typing yes or no");
alert("Finally the total cost should appear in your box");
alert("FAQ What happens if I have entered the wrong length... keep pressing cancel and restart");
alert("FAQ What happens if my house has three walls ... type in three lengths and press 0 for the fourth");
alert("FAQ What happens if i want one wall one colour and another wall the other...... At the moment this app cant calculate that");
};

var height = prompt ("Please enter Your Height");
var lengthArray = [];
for ( i = 0 ; i<4; i ++ ){
var length = prompt ("Please enter your lengths");
lengthArray.push(length);
console.log(lengthArray);
var area = height * length;
}
// Paint Cost
var Economy = 0.45;
var Standard = 1.00;
var Luxury = 1.45;
var underCoat = 0.50;
var paintCost;
var undercoatCost;

// The if stament for the standard costs
var paintChoice = prompt (" Please Choose your paint ");
if (paintChoice === "Standard"){
paintCost = (Standard*area);
console.log("The cost of this paint will be " + paintCost);
}
// The if stament for the standard costs
else if (paintChoice === "Economy"){
paintCost = (Economy*area);
console.log("The cost of this paint will be " + paintCost);
}
// The if stament for the standard costs
else if (paintChoice === "Luxury"){
paintCost = (Luxury*area);
console.log("The cost of this paint will be " + paintCost);
}
// This is the else if stament for the choice of selecting a wrong colour
else if (paintChoice === "Red"){
console.log("You have chosen the wrong paint choice ");
}
// This is the the if stament for undercoat when you choose yes
var underCoat = prompt ("Would you like any undercoat? ");
if (underCoat === "Yes"){
undercostCost = (area*underCoat);
console.log("The cost of the undercoat will be " + undercoatCost);
}
// This is the else if statement for undercoat when you choose no
else if (underCoat === "No"){
console.log("You have chosen not to have undercoat ");
}
// This is the variable to total up the lengths
var total = [];
var lengthSum = lengthArray[0] + lengthArray[1] + lengthArray[2] + lengthArray[3];
console.log(lengthSum);
var area = (height*lengthSum);
alert("The area of the room is " + area.toFixed(2));
#2. Posted:
-Pawn
  • Spooky Poster
Status: Offline
Joined: Jan 15, 201113Year Member
Posts: 2,316
Reputation Power: 119
Status: Offline
Joined: Jan 15, 201113Year Member
Posts: 2,316
Reputation Power: 119
First, I'd say calculate the area more towards the beginning where a user defines what the lengths of his room are, etc. So this right here:

// This is the variable to total up the lengths
var total = [];
var lengthSum = lengthArray[0] + lengthArray[1] + lengthArray[2] + lengthArray[3];
console.log(lengthSum);
var area = (height*lengthSum);
alert("The area of the room is " + area.toFixed(2));

Should go after:

var height = prompt ("Please enter Your Height");
var lengthArray = [];
for ( i = 0 ; i<4; i ++ ){
var length = prompt ("Please enter your lengths");
lengthArray.push(length);
console.log(lengthArray);
var area = height * length;
}

I don't know if this will fix the problems, but I'm pretty sure it will address some of them. I'm not amazing with Javascript, but I'm decent with it. I hope I helped in someway. As to his recommendation of not using two variables declared with the same name, I didn't even see two variables declared with the same name, unless they were called in different sections of the code.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.