
- 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
Is the digit divisible by the previous digit of the number in JavaScript
Problem
We are required to write a JavaScript function that takes in a number and checks each digit if it is divisible by the digit on its left and returns an array of booleans.
The booleans should always start with false because there is no digit before the first one.
Example
Following is the code −
const num = 73312; const divisibleByPrevious = (n = 1) => { const str = n.toString(); const arr = [false]; for(let i = 1; i < str.length; ++i){ if(str[i] % str[i-1] === 0){ arr.push(true); }else{ arr.push(false); }; }; return arr; }; console.log(divisibleByPrevious(num));
Output
[ false, false, true, false, true ]
- Related Questions & Answers
- C++ Program for the Largest K digit number divisible by X?
- Largest K digit number divisible by X in C++
- Find nth number that contains the digit k or divisible by k in C++
- Count n digit numbers divisible by given number in C++
- C++ Program for Largest K digit number divisible by X?
- C++ Program for Smallest K digit number divisible by X?
- C++ Programming for Smallest K digit number divisible by X?
- Python Program for Smallest K digit number divisible by X
- Java Program for Largest K digit number divisible by X
- Java Program for Smallest K digit number divisible by X
- Largest N digit number divisible by given three numbers in C++
- N digit numbers divisible by 5 formed from the M digits in C++
- Checking digit sum of smallest number in the array in JavaScript
- Finding the largest 5 digit number within the input number using JavaScript
- JavaScript - Find the smallest n digit number or greater
Advertisements