JavaScript: Check from three given numbers that two or all of them have the same rightmost digit
JavaScript Basic: Exercise-43 with Solution
Write a JavaScript program to check from three given numbers (non negative integers) that two or all of them have the same rightmost digit.
This JavaScript program checks whether from three given non-negative integers, two or all of them have the same rightmost digit. It examines the last digit of each number and compares them to determine if they match, returning true if there's a match and false otherwise.
Visual Presentation:
Sample Solution:
JavaScript Code:
// Define a function named same_last_digit with parameters p, q, and r
function same_last_digit(p, q, r) {
// Return true if the last digit of p is equal to the last digit of q or r,
// or if the last digit of q is equal to the last digit of r
return (p % 10 === q % 10) ||
(p % 10 === r % 10) ||
(q % 10 === r % 10);
}
// Log the result of calling same_last_digit with the arguments 22, 32, and 42 to the console
console.log(same_last_digit(22, 32, 42));
// Log the result of calling same_last_digit with the arguments 102, 302, and 2 to the console
console.log(same_last_digit(102, 302, 2));
// Log the result of calling same_last_digit with the arguments 20, 22, and 45 to the console
console.log(same_last_digit(20, 22, 45));
Output:
true true false
Live Demo:
See the Pen JavaScript: Check from three given numbers - basic-ex-43 by w3resource (@w3resource) on CodePen.
Flowchart:
ES6 Version:
// Define a function named same_last_digit using arrow function syntax with parameters p, q, and r
const same_last_digit = (p, q, r) => {
// Check if the last digit of p is equal to the last digit of q or r
// Check if the last digit of q is equal to the last digit of r
return (p % 10 === q % 10) || (p % 10 === r % 10) || (q % 10 === r % 10);
};
// Log the result of calling same_last_digit with the arguments 22, 32, and 42 to the console
console.log(same_last_digit(22, 32, 42));
// Log the result of calling same_last_digit with the arguments 102, 302, and 2 to the console
console.log(same_last_digit(102, 302, 2));
// Log the result of calling same_last_digit with the arguments 20, 22, and 45 to the console
console.log(same_last_digit(20, 22, 45));
Improve this sample solution and post your code through Disqus.
Previous: JavaScript program to check if three given numbers are increasing in strict mode or in soft mode.
Next: JavaScript program to check from three given integers that if a number is greater than or equal to 20 and less than one of the others.
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-43.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics