JavaScript Code

Smallest of Three Numbers Program


Problem Statement

write a JavaScript program with a function that takes three numbers as arguments, finds the smallest of these three numbers, and returns the smallest number

Solution

consider that num1, num2, and num3 are the three numbers

if num1 is less than both num2 and num3, then num1 is the smallest,

else if num2 is less than num3, then num2 is the smallest,

else num3 is the smallest

Program

1. find smallest of three numbers using if-else-if statement

function findSmallest(num1, num2, num3) {
    if (num1 < num2 && num1 < num3) {
        return num1;
    } else if (num2 < num3) {
        return num2;
    } else {
        return num3;
    }
}

var num1 = 5;
var num2 = 8;
var num3 = 2;

const result = findSmallest(num1, num2, num3);
console.log('Smallest : ' + result);