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

Evaluate if Integer is ≥20 and Less Than Another

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.

This JavaScript program evaluates three given integers to determine if one number is greater than or equal to 20 and less than the other two. It compares each number against 20 and ensures that it is not greater than the other two numbers. If these conditions are met for any number, it returns true; otherwise, it returns false.

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript-exercises/javascript-basic-exercise-44.php