1

im trying to figure out why my if statements aren't being displayed on my website. My first script is working which is displaying the triangles areas, and i would like to try to find out how i can display which triangle is bigger using ifelse statements. this is my first time trying to utilize these and would like some help.

edit: I have fixed my paranthesis on my if statements, but now its displaying them even if theyre not true. how should i go about solving this?

edit2: THANK YOU ALL for the amazing help. i appreciate it, my code is now working!

<script>
var Base1 = parseFloat(prompt("Base1", "100"));
var Height1 = parseFloat(prompt("Height1", "100"));
var Base2 = parseFloat(prompt("Base2", "100"));
var Height2 = parseFloat(prompt("Height2", "100"));

document.write("<p>triangle1's Volume is : "+Base1 * Height1/2+"</p>");
document.write("<p>triangle2's Volume is : "+Base2 * Height2/2+"</p>");
</script>
<script>
if((Base1 * Height1/2) == (Base2 * Height2/2));
{
  document.write("<p>the triangles are the same size</p>");
}
if ((Base1 * Height1/2) > (Base2 * Height2/2));
{
    document.write("<p>triangle 1 is bigger</p>");  
}
if ((Base1 * Height1/2) < (Base2 * Height2/2)); 
{
    document.write("<p>triangle2 is bigger</p>");
}
  
</script> 

3 Answers3

0

Your if conditions had improper syntax:

<script>
var Base1 = parseFloat(prompt("Base1", "100"));
var Height1 = parseFloat(prompt("Height1", "100"));
var Base2 = parseFloat(prompt("Base2", "100"));
var Height2 = parseFloat(prompt("Height2", "100"));

document.write("<p>triangle1's Volume is : "+Base1 * Height1/2+"</p>");
document.write("<p>triangle2's Volume is : "+Base2 * Height2/2+"</p>");
</script> 

<script>
if((Base1 * Height1/2) === (Base2 * Height2/2))
{
  document.write("<p>the triangles are the same size</p>");
} else if ((Base1 * Height1/2) > (Base2 * Height2/2))
{
    document.write("<p>triangle 1 is bigger</p>");  
}
else
{
    document.write("<p>triangle2 is bigger</p>");
}
</script>
coltonrusch
  • 194
  • 11
  • why should i use "===" rather than "==" here? – Style123112 Aug 04 '20 at 21:39
  • In this case, you should be fine using either. However, [using "===" is often considered a best practice](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) in case there is some tricky type coercion going on. – coltonrusch Aug 04 '20 at 21:43
  • [It is just good practice in general](https://www.impressivewebs.com/why-use-triple-equals-javascipt/), has nothing to do with your problem right now. – CherryDT Aug 04 '20 at 21:45
0

Just a typo.. Two fixes:

  1. you were missing parenthesis on the if statements.
  2. remove ; from between the end of the if statement and the opening of the block.

<script>
var Base1 = parseFloat(prompt("Base1", "100"));
var Height1 = parseFloat(prompt("Height1", "100"));
var Base2 = parseFloat(prompt("Base2", "100"));
var Height2 = parseFloat(prompt("Height2", "100"));

document.write("<p>triangle1's Volume is : "+Base1 * Height1/2+"</p>");
document.write("<p>triangle2's Volume is : "+Base2 * Height2/2+"</p>");
</script>
<script>
if((Base1 * Height1/2) == (Base2 * Height2/2))
{
  document.write("<p>the triangles are the same size</p>")
}
if((Base1 * Height1/2) > (Base2 * Height2/2))
{
    document.write("<p>triangle 1 is bigger</p>")
}
if ((Base1 * Height1/2) > (Base2 * Height2/2))
{
    document.write("<p>triangle2 is bigger</p>")
}
  
</script>
SomoKRoceS
  • 2,934
  • 2
  • 19
  • 30
0

You just had some syntax errors. You can see all errors if you use the developer tools. I added comments where the errors were.

<script>
  var Base1 = parseFloat(prompt("Base1", "100"));
  var Height1 = parseFloat(prompt("Height1", "100"));
  var Base2 = parseFloat(prompt("Base2", "100"));
  var Height2 = parseFloat(prompt("Height2", "100"));

  document.write("<p>triangle1's Volume is : " + Base1 * Height1 / 2 + "</p>");
  document.write("<p>triangle2's Volume is : " + Base2 * Height2 / 2 + "</p>");
</script>
<script>
  if ((Base1 * Height1 / 2) == (Base2 * Height2 / 2)) // added brackets
  {
    document.write("<p>the triangles are the same size</p>");
  }
  if ((Base1 * Height1 / 2) > (Base2 * Height2 / 2)) // removed ; and added brackets
  {
    document.write("<p>triangle 1 is bigger</p>");
  }
  if ((Base1 * Height1 / 2) > (Base2 * Height2 / 2)) // removed ; and added brackets
  {
    document.write("<p>triangle2 is bigger</p>");
  }
</script>
NoNickAvailable
  • 398
  • 1
  • 2
  • 12