Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to compare 3 values in Javascript?

Hi guys!

I'am doing an exercise where I have to compare 3 values and printout what kind of triangle it was. So far this are my code. The last one is tricky. Can't figure out how to compare 3 values that only 2 value are equal.

Greatly appreciate your help!

/*
Tasks:
1. ask for 3 numbers from user. 
2. If user enters 3 equal #. Display a message that the type of triangle is Equalatiral.
3. If Only 2 # are equal. Display a message that type of triangle is Isosceles.
4. If no equal # are entered. Display a message that type of triangle is Scalene.
*/
function verifyTriangleType(num1, num2, num3){

    var n1 = num1;
    var n2 = num2;
    var n3 = num3;

    if ( n1 === n2 && n2 === n3 && n1 === n3){
        console.log('Equalatiral Triangle');
    } else if( n1 != n2 || n2 != n3 || n3 != n1){
        console.log('Scalene Triangle');
    } else if( HELP! GOT STUCK HERE task # 3 ){

    }
}

verifyTriangleType(5, 5, 5);
verifyTriangleType(5, 6, 10);
verifyTriangleType(7, 5, 58);
verifyTriangleType(3, 7, 5);
verifyTriangleType(5, 5, 5);

Owa, I like your icon. I'm guessing you did it yourself. Nice work :D

Thanks John! yes i did it myself. Looks like me in someway. hehe

Cheers!

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

This answer was edited for accuracy

Hi there! John made an excellent effort! And I had originally posted that his solution works. However, it fails in certain circumstances (as evidenced by the last two verification data sets). His code will incorrectly output that they are Scalene Triangles when they should be Isosceles.

/*
Tasks:
1. ask for 3 numbers from user. 
2. If user enters 3 equal #. Display a message that the type of triangle is Equalatiral.
3. If Only 2 # are equal. Display a message that type of triangle is Isosceles.
4. If no equal # are entered. Display a message that type of triangle is Scalene.
*/
function verifyTriangleType(num1, num2, num3){

    var n1 = num1;
    var n2 = num2;
    var n3 = num3;

   if (n1 === n2 && n1 === n3) {
       console.log("Equalateral Triangle");
   } else if (n1 === n2 || n1 === n3 || n2 === n3) {
       console.log("Isoceles Triangle");
   } else {
       console.log("Scalene Triangle");
   }
 }



verifyTriangleType(5, 5, 5);
verifyTriangleType(5, 6, 10);
verifyTriangleType(7, 5, 58);
verifyTriangleType(3, 7, 5);
verifyTriangleType(5, 5, 5);
verifyTriangleType(4, 3, 4);
verifyTriangleType(5, 8, 8);

Hope this helps! And sorry, john larson :smiley:

Jennifer, Yours is much more concise :D I like it.

I was about to make a comment that one should note that if n1 === n2 and n2 === n3, then n1 === n3 is a given, since n1 === n2.

In the second case, one can note that n1, n2 and n3 are not equal, because if they were, we would have gone into the first if statement, therefore we only have to check pairs.

Nice code, you should mark it as the best answer =)

Im about to say I was confused but after reading Kristian notes I was able to understand that I have only to compare two numbers for the Isosceles Triangle. Because theoretically Isosceles only has 2 equal number and even which sides of the triangle are equal will do.

Thank you both of you! :)

//This is as far as I got and my head started to hurt.  
//Maybe you can look at it and take it further
//It might even work I don't know
//It ran and I didn't get any errors

/*
Tasks:
1. ask for 3 numbers from user.
2. If user enters 3 equal #. Display a message that the type of triangle is Equalatiral.
3. If Only 2 # are equal. Display a message that type of triangle is Isosceles.
4. If no equal # are entered. Display a message that type of triangle is Scalene.
*/
function verifyTriangleType(num1, num2, num3){

    var n1 = num1;
    var n2 = num2;
    var n3 = num3;

    if ( n1 === n2 && n2 === n3 && n1 === n3){
        console.log('Equalatiral Triangle');
    } else if( n1 != n2 || n2 != n3 || n3 != n1){
        console.log('Scalene Triangle');
    } else if( n1 === n2 && n1 !==n3 && n2 !== n3  ){

    }
}

verifyTriangleType(5, 5, 5);
verifyTriangleType(5, 6, 10);
verifyTriangleType(7, 5, 58);
verifyTriangleType(3, 7, 5);
verifyTriangleType(5, 5, 5);

Thank you john! really appreciate your help.