Challenge: Find the Largest of Three Numbers

Challenge: Find the Largest of Three Numbers

Logic Puzzle: Ready to flex those coding muscles?

The Challenge

Alright Zzina crew, here’s a brain teaser for you! You’ve got three numbers – A, B, and C. Your mission, should you choose to accept it, is to figure out which one is the largest. BUT, there’s a catch! You can ONLY use IF/ELSE statements. No sneaky shortcuts!

Think you can handle it? Don’t let the simplicity fool you; it’s all about the logic!

A Tiny Hint…

Okay, okay, I’ll throw you a bone. Start by comparing two numbers. Once you know which one is bigger, compare that one to the remaining number. Got it? 😉

The Solution

Alright, let’s see if you cracked it! Here’s one way you could write the code:


                function findLargest(a, b, c) {
                    if (a >= b && a >= c) {
                        return a;
                    } else if (b >= a && b >= c) {
                        return b;
                    } else {
                        return c;
                    }
                }

                // Example usage:
                let largest = findLargest(10, 5, 8);
                console.log("The largest number is: " + largest); // Output: 10
            

Explanation: The code checks if ‘a’ is greater than or equal to both ‘b’ and ‘c’. If it is, ‘a’ is returned as the largest. Otherwise, it checks if ‘b’ is greater than or equal to both ‘a’ and ‘c’, returning ‘b’ if true. Finally, if neither ‘a’ nor ‘b’ is the largest, ‘c’ must be, and is returned.

Brain Check!

So, did you solve it? Even if you didn’t get it exactly right, the important thing is that you’re thinking logically! Now, here’s a bonus question: How would you modify this code to handle negative numbers? 🤔 Let me know in the comments!

Share this Post

Leave a Reply

Your email address will not be published. Required fields are marked *