JavaScript: Find the larger number from the two given positive integers
JavaScript Basic: Exercise-34 with Solution
Write a JavaScript program to find the largest number from the two given positive integers. The two numbers are in the range 40..60 inclusive.
The JavaScript program finds the largest number between two given positive integers, ensuring both numbers are within the range of 40 to 60 inclusive. If both numbers are within the range, it returns the larger number; otherwise, it handles invalid input.
Visual Presentation:
Sample Solution:
JavaScript Code:
// Define a function named max_townums_range with parameters x and y
function max_townums_range(x, y) {
// Check if x and y fall within the specified range using logical AND and comparison operators
if (x >= 40 && x <= 60 && y >= 40 && y <= 60) {
// Check if x and y are the same
if (x === y) {
return "Numbers are the same";
} else if (x > y) {
return x; // Return x if it is greater than y
} else {
return y; // Return y if it is greater than x
}
} else {
return "Numbers don't fit in range"; // Return this message if numbers are outside the range
}
}
// Log the result of calling max_townums_range with the arguments 45 and 60 to the console
console.log(max_townums_range(45, 60));
// Log the result of calling max_townums_range with the arguments 25 and 60 to the console
console.log(max_townums_range(25, 60));
// Log the result of calling max_townums_range with the arguments 45 and 80 to the console
console.log(max_townums_range(45, 80));
Output :
60 Numbers don't fit in range Numbers don't fit in range
Live Demo:
See the Pen javascript-basic-exercise-34 by w3resource (@w3resource) on CodePen.
Flowchart:
ES6 Version:
// Define a function named max_townums_range using arrow function syntax with parameters x and y
const max_townums_range = (x, y) => {
// Check if x and y fall within the specified ranges using logical AND operators
if (x >= 40 && x <= 60 && y >= 40 && y <= 60) {
// Check if x is equal to y
if (x === y) {
return "Numbers are the same";
} else if (x > y) {
return x;
} else {
return y;
}
} else {
return "Numbers don't fit in range";
}
};
// Log the result of calling max_townums_range with the arguments 45 and 60 to the console
console.log(max_townums_range(45, 60));
// Log the result of calling max_townums_range with the arguments 25 and 60 to the console
console.log(max_townums_range(25, 60));
// Log the result of calling max_townums_range with the arguments 45 and 80 to the console
console.log(max_townums_range(45, 80));
Improve this sample solution and post your code through Disqus.
Previous: JavaScript program to check if two numbers are in range 40..60 or in the range 70..100 inclusive.
Next: JavaScript program to check a given string contains 2 to 4 numbers of a specified character.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
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-34.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics