1 Replies - 493 Views - Last Post: 21 October 2016 - 09:13 AM Rate Topic: -----

#1 PhoenixFox   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-October 16

Beginner question about if statements and efficiency

Posted 21 October 2016 - 09:10 AM

My main question is how you add other variables into a boolean expression where your asking if one variable is greater than for example 4 or 5 others.

Here is my code so far, it doesent seem to be working. If you know of more efficient ways to write this code I'm open to any suggestions as I'm a total noob with java.

var temp1, temp2, temp3, temp4, temp5;
var day1, day2, day3, day4, day5;
var avg;
var highest;
var lowest;
var ES = "";
var BR = "</br>";


temp1 = prompt ("Employee, please enter the temperature in celcius that it was at noon on Monday", ES );
temp1 = parseInt (temp1);

temp2 = prompt ("Employee, please enter the temperature in celcius that it was at noon on Tuesday", ES );
temp2 = parseInt (temp2);

temp3 = prompt ("Employee, please enter the temperature in celcius that it was at noon on Wednesday", ES );
temp3 = parseInt (temp3);

temp4 = prompt ("Employee, please enter the temperature in celcius that it was at noon on Thursday", ES );
temp4 = parseInt (temp4);

temp5 = prompt ("Employee, please enter the temperature in celcius that it was at noon on Friday", ES );
temp5 = parseInt (temp5);

if (temp1 > temp2, temp3, temp4, temp5) {
	temp1 = highest;
}

	else if (temp2 > temp1, temp3, temp4, temp5) {
		temp2 = highest;
	}
	
	else if (temp3 > temp1, temp2, temp4, temp5) {
		temp2 = highest;
	}

	else if (temp4 > temp1, temp3, temp2, temp5) {
		temp2 = highest;
	}

	else if (temp5 > temp1, temp3, temp4, temp2) {
		temp2 = highest;
	}

	
if (temp1 < temp2, temp3, temp4, temp5) {
	temp1 = lowest;
}

	else if (temp2 < temp1, temp3, temp4, temp5) {
		temp2 = lowest;
	}
	
	else if (temp3 < temp1, temp2, temp4, temp5) {
		temp2 = lowest;
	}

	else if (temp4 < temp1, temp3, temp2, temp5) {
		temp2 = lowest;
	}

	else if (temp5 < temp1,temp3,temp4,temp2) {
		temp2 = lowest;
	}


avg = temp1 + temp2 + temp3 + temp4 + temp5 / 5

document.write("Welcome to the temperature calculater program!" + BR);
document.write("The highest recorded temperature is " + highest + BR);
document.write("The lowest recorded temperature is " + lowest + BR);
document.write("The mean temperature is " + avg + BR);
document.write("Thanks for using the program and have a great day!");
:code:

This post has been edited by modi123_1: 21 October 2016 - 09:10 AM
Reason for edit:: In the future, please use the '[code]' tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: Beginner question about if statements and efficiency

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Beginner question about if statements and efficiency

Posted 21 October 2016 - 09:13 AM

When you have an if condition like this, what do you expect is happening?
25if (temp1 > temp2, temp3, temp4, temp5) {

Typically you would need to specify both sides of the comparison for each comparison you want and string them together using AND or OR.

if a > b and a > c and a > d.. 


I personally would use a collection.. like an array or list to hold all the values and just find the max, min, average, etc using a for loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1