

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Comparing integers by taking two numbers in JavaScript
We are required to write a JavaScript function that takes in two numbers, let’s say num1 and num2.
If num1 is greater than num2, our function should return greater.
If num2 is greater than num1, our function should return smaller.
Otherwise, the function should return equal.
Example
Following is the code −
const compareIntegers = (num1, num2) => { if(typeof num1 !== 'number' || typeof num2 !== 'number'){ return false; }; if(num1 > num2){ return 'greater'; }else if(num2 > num1){ return 'smaller'; }else{ return 'equal'; }; }; console.log(compareIntegers(12, 56)); console.log(compareIntegers(72, 56)); console.log(compareIntegers(12, 12)); console.log(compareIntegers(12, 33));
Output
Following is the output on console −
smaller greater equal smaller
- Related Questions & Answers
- Taking part from array of numbers by percent JavaScript
- Armstrong Numbers between two integers?
- Maximum Product of Two Numbers in a List of Integers in JavaScript
- Comparing corresponding values of two arrays in JavaScript
- Comparing two strings in MySQL?
- Comparing two strings in C++
- Comparing two dates in PHP
- Taking the absolute sum of Array of Numbers in JavaScript
- Random whole number between two integers JavaScript
- Comparing two Strings lexicographically in Java
- JavaScript algorithm for converting integers to roman numbers
- Comparing forEach() and reduce() for summing an array of numbers in JavaScript.
- Add two numbers represented by two arrays in C Program
- JavaScript program to convert positive integers to roman numbers
- Divide Two Integers in C++
Advertisements