w3resource

JavaScript: Check from three given integers that whether a number is greater than or equal to 20 and less than the other two

JavaScript Basic: Exercise-44 with Solution

Write a JavaScript program that evaluates three given integers to determine if any one of them is greater than or equal to 20 and less than the other two.

Visual Presentation:

JavaScript: Check from three given integers that whether a number is greater than or equal to 20 and less than one of the others

Sample Solution:

JavaScript Code:

// Define a function named lessby20_others with parameters x, y, and z
function lessby20_others(x, y, z) {
  // Check if x is greater than or equal to 20 and (x is less than y or x is less than z)
  // Check if y is greater than or equal to 20 and (y is less than x or y is less than z)
  // Check if z is greater than or equal to 20 and (z is less than y or z is less than x)
  return (
    (x >= 20 && (x < y || x < z)) ||
    (y >= 20 && (y < x || y < z)) ||
    (z >= 20 && (z < y || z < x))
  );
}

// Log the result of calling lessby20_others with the arguments 23, 45, and 10 to the console
console.log(lessby20_others(23, 45, 10));

// Log the result of calling lessby20_others with the arguments 23, 23, and 10 to the console
console.log(lessby20_others(23, 23, 10));

// Log the result of calling lessby20_others with the arguments 21, 66, and 75 to the console
console.log(lessby20_others(21, 66, 75));

Output:

true
false
true

Live Demo:

See the Pen JavaScript: Check from three given integers that whether a number is greater than or equal to 20 and less than one of the others - basic-ex-44 by w3resource (@w3resource) on CodePen.


Flowchart:

Flowchart: JavaScript - Check from three given integers that if a number is greater than or equal to 20 and less than one of the others

ES6 Version:

// Define a function named lessby20_others using arrow function syntax with parameters x, y, and z
const lessby20_others = (x, y, z) => {
  // Return true if x is greater than or equal to 20 and (x is less than y or x is less than z)
  // Return true if y is greater than or equal to 20 and (y is less than x or y is less than z)
  // Return true if z is greater than or equal to 20 and (z is less than y or z is less than x)
  return (x >= 20 && (x < y || x < z)) ||
         (y >= 20 && (y < x || y < z)) ||
         (z >= 20 && (z < y || z < x));
};

// Log the result of calling lessby20_others with the arguments 23, 45, and 10 to the console
console.log(lessby20_others(23, 45, 10));

// Log the result of calling lessby20_others with the arguments 23, 23, and 10 to the console
console.log(lessby20_others(23, 23, 10));

// Log the result of calling lessby20_others with the arguments 21, 66, and 75 to the console
console.log(lessby20_others(21, 66, 75));

Improve this sample solution and post your code through Disqus.

Previous: JavaScript program to check from three given numbers (non negative integers) that two or all of them have the same rightmost digit.
Next: JavaScript program to check two given integer values and return true if one of the number is 15 or if their sum or difference is 15.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.