w3resource

JavaScript: Check whether a given positive number is a multiple of 3 or a multiple of 7

JavaScript Basic: Exercise-25 with Solution

Write a JavaScript program to check whether a given positive number is a multiple of 3 or 7.

This JavaScript program checks if a given positive number is a multiple of either 3 or 7. It uses the modulo operator (%) to determine if the number is divisible by 3 or 7 without any remainder. If the result is 0 for either operation, it means the number is a multiple of 3 or 7, respectively.

Visual Presentation:

JavaScript: Check if a given positive number is a multiple of 3 or a multiple of 7

Sample Solution:

JavaScript Code:

// Define a function named test37 that takes a parameter x
function test37(x) {
  // Check if x is divisible by 3 or 7
  if (x % 3 == 0 || x % 7 == 0) {
    // If true, return true
    return true;
  } 
  // If not divisible by 3 or 7, return false
  else {
    return false;
  }
}

// Log the result of calling the test37 function with the argument 12 to the console
console.log(test37(12));

// Log the result of calling the test37 function with the argument 14 to the console
console.log(test37(14));

// Log the result of calling the test37 function with the argument 10 to the console
console.log(test37(10));

// Log the result of calling the test37 function with the argument 11 to the console
console.log(test37(11)); 

Output:

true
true
false
false

Live Demo:

See the Pen JavaScript: check if a given positive number is a multiple of 3 or 7.-basic-ex-25 by w3resource (@w3resource) on CodePen.


Flowchart:

Flowchart: JavaScript - Check whether a given positive number is a multiple of 3 or a multiple of 7

ES6 Version:

// Using ES6 arrow function syntax to define the test37 function
const test37 = (x) => {
  // Check if x is divisible by 3 or 7
  if (x % 3 == 0 || x % 7 == 0) {
    // If true, return true
    return true;
  } 
  // If not divisible by 3 or 7, return false
  else {
    return false;
  }
};

// Log the result of calling the test37 function with the argument 12 to the console
console.log(test37(12));

// Log the result of calling the test37 function with the argument 14 to the console
console.log(test37(14));

// Log the result of calling the test37 function with the argument 10 to the console
console.log(test37(10));

// Log the result of calling the test37 function with the argument 11 to the console
console.log(test37(11));

Improve this sample solution and post your code through Disqus.

Previous: JavaScript program to create a new string from a given string with the first character of the given string added at the front and back.
Next: JavaScript program to create a new string from a given string taking the last 3 characters and added at both the front and back.

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-25.php