Using only conditional statements in JavaScript to write a code that solves the said problem requires a quite good understanding of if-else conditional statements. Solving the problem with the help of loops and the other available functions is not what we are aiming for in this tutorial. This article is focused on solving the problem without using any of the advanced concepts. The task can be accomplished with the help of two methods. One requires us to strictly use only conditional statements. And the other method is quite manageable as it allows the use of conditional statements as well as logical operators.

Find the Largest of 5 numbers: Conditional Statements in JavaScript
Find the largest of 5 numbers: Conditional Statements in JavaScript

Problem Statement: Write a program in JS to find the largest among 5 numbers without using loops.

This problem majorly requires us to use our concentration and the ability to write neat code. It is an easy problem to solve, just try and understand to follow the steps:

There are two methods to solve this problem without using loops and functions. The first method requires the use of only conditional statements. The second method requires the use of conditional statements as well as logical operators.

Method-1 ( Using Only Conditional Statements in JavaScript)

Following is the first approach to solve the said problem. We only focus on using conditional statements in this solution, which makes it quite a challenging task.

The HTML document will just have a submit button on it. On clicking “submit”, the JavaScript onclick event will execute the function. The function aims to take 5 different numbers as input from the user. It is programmed to store each of the inputs as separate variables. After which, the function will work with the logic to check the greatest of all the input numbers that it has received.

The first piece of code is just the boilerplate code of HTML. We have used a simple input type “submit” in our code. The JavaScript code is incorporated with the help of the “onclick” event. This event further calls out a function called “five_max().

HTML Code – Method 1

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Largest Number Among 5 Numbers</title>
    <script type="text/javascript">
    
    </script>
</head>
<body>
    <input type="submit" name="" onclick="five_max()">
</body>
</html>

JavaScript Code – Method 1

This is the JavaScript code that you have to put inside the script tag of the boilerplate. The onclick event which we have used on our HTML submit button will execute this script upon being clicked on.


    <script type="text/javascript">
    function five_max() {
    var a = parseInt(prompt("Enter first number :"));
    var b = parseInt(prompt("Enter second number :"));
    var c = parseInt(prompt("Enter third number :"));
    var d = parseInt(prompt("Enter fourth number :"));
    var e = parseInt(prompt("Enter fifth number :"));

    if (a > b) {
        if (a > c) {
            if (a > d) {
                if (a > e) {
                    alert("The max number is " + a);
                } else {
                    alert("The max number is " + e);
                }
            } else if (d > e) {
                alert("The max number is " + d);
            } else {
                alert("The max number is " + e);
            }
        } else if (c > d) {
            if (c > e)
            {
              alert("The max number is " + c);
            } else {
                alert("The max number is " + e);
            }
        } else if (d > e) {
            alert("The max number is " + d);
        } else {
            alert("The max number is " + e);
        }
    } else if (b > c) {
        if (b > d) {
            if (b > e) {
                alert("The max number is " + b);
            } else {
                alert("The max number is " + e);
            }
        } else if (d > e) {
            alert("The max number is " + d);
        } else {
            alert("The max number is " + e);
        }
    } else if (c > d) {
        if (c > e) {
            alert("The max number is " + c);
        } else {
            alert("The max number is " + e);
        }
    } else if (d > e) {
        alert("The max number is " + d);
    } else {
        alert("The max number is " + e);
    }
    }
    </script>

This method is very basic which only uses conditional statements of JavaScript. But it is a popular interview problem since it shows a great understanding of the concept.

Method-2 (Using Conditional Statements in JavaScript Along with Logical Operators)

This method is a more simplified version of the above code. It is a very direct and easy-to-understand code. Logical Operators in JS are and, or and not. This method will straight away check all the different expressions with each of the conditional statements. The HTML Code for both the methods is same. We have even used the same function name, so there’s not even a slight change in the HTML code.

HTML Code – Method 2

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Largest Number Among 5 Numbers</title>
    <script type="text/javascript">
    
    </script>
</head>
<body>
    <input type="submit" name="" onclick="five_max()">
</body>
</html>

JavaScript Code – Method 2

<script type="text/javascript">
   function five_max() {
    var a = parseInt(prompt("Enter first number: "));
    var b = parseInt(prompt("Enter second number: "));
    var c = parseInt(prompt("Enter third number: "));
    var d = parseInt(prompt("Enter fourth number: "));
    var e = parseInt(prompt("Enter fifth number: "));

    if (a > b && a > c && a > d && a > e) {
       alert("The max number is " + a);
    } else if (b > a && b > c && b > d && b > e) {
       alert("The max number is " + b);
    } else if (c > a && c > b && c > d && c > e) {
       alert("The max number is " + c);
    } else if (d > a && d > b && d > c && d > e) {
       alert("The max number is " + d);
    } else {
       alert("The max number is " + e);
    }
  }
</script>      

This method follows the use of a logical and operator at every condition, which makes it a really simple and easy solution to work with.

So that’s it for this problem, we are working on more such problem statements to come up with in the future. This problem can be easily targeted with a loop or a function, but the main focus was to solve this without using the two. You can use the code and modify it according to your need.

Do share the article and your review in the comments down below.

Check out my other blog posts as well:


Vaishali Rastogi

Hey There! I am Vaishali Rastogi and I am a tech-researcher. I've been doing writing for a good 4-5 years, so here I am now. Working on my own website to make people digitally aware of the updates in technology.

0 Comments

Leave a Reply

Avatar placeholder

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

nine + six =