
- 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
Checking for coprime numbers in JavaScript
Two numbers are said to be co-primes if there exists no common prime factor amongst them (1 is not a prime number).
We are required to write a function that takes in two numbers and returns true if they are coprimes otherwise returns false.
Example
The code for this will be −
const areCoprimes = (num1, num2) => { const smaller = num1 > num2 ? num1 : num2; for(let ind = 2; ind < smaller; ind++){ const condition1 = num1 % ind === 0; const condition2 = num2 % ind === 0; if(condition1 && condition2){ return false; }; }; return true; }; console.log(areCoprimes(4, 5)); console.log(areCoprimes(9, 14)); console.log(areCoprimes(18, 35)); console.log(areCoprimes(21, 57));
Output
The output in the console −
true true true false
- Related Questions & Answers
- JavaScript - Checking for pandigital numbers
- Checking for Fibonacci numbers in JavaScript
- Checking for special numbers in JavaScript
- Checking for co-prime numbers - JavaScript
- Checking for the Gapful numbers in JavaScript
- Checking semiprime numbers - JavaScript
- Checking for vowels in array of numbers using JavaScript
- Checking Oddish and Evenish numbers - JavaScript
- Checking for overlapping times JavaScript
- Checking for string anagrams JavaScript
- Checking for ascending arrays in JavaScript
- Checking for straight lines in JavaScript
- Checking for convex polygon in JavaScript
- Checking for increasing triplet in JavaScript
- JavaScript Array: Checking for multiple values
Advertisements