Jumbled Numbers JavaScript

Jumbled Numbers JavaScript | In this post, we will see a program to check whether the given number is a Jumbled number or not in JavaScript.

A Jumbled number is one in which the neighboring digits (on the left or right) deviate by at least one value. This article will help you learn how to deal with Jumbled numbers in JavaScript. Here are a few examples that will help you understand the jumbled numbers in JavaScript.

Example 1:
number-  12345
Is it a Jumbled number?
True

Example 2: 
7898
Is it a Jumbled number?
True

Example 3: 
87582
Is it a Jumbled number?
False

Example 4:
2439
Is it a Jumbled number?
False

Steps To Find Out Jumbled Numbers In JavaScript

1. For a program in JavaScript to find jumbled numbers, first declare the function. 
2. Using the if-else and while loop, parse each digit and check whether or not their adjacent values are greater than one.
3. Declare two conditions for digit at “i” and at [i-1].

4. Use the if loop for conditions where the difference is greater than 1.
5. Check whether the conditions are satisfied.
6. Take inputs or declare the numbers you want to check. Then give a condition using an if-else loop, where it is True when the numbers are jumbled or False when the numbers are not jumbled.

Program To Find Out Jumbled Numbers JavaScript

// Jumbled numbers in JavaScript

// Function declaration 
function checkJumbled(n) {

   // for single digit number
   if (parseInt(n / 10, 10) == 0)
      return true;

   // parsing each digit via loop
   while (n != 0) {

      //checked
      if (parseInt(n / 10, 10) == 0)
         return true;

      // number present at index i
      let digit1 = n % 10;

      // number present at index i-1
      let digit2 = parseInt(n / 10, 10) % 10;

      // if loop for conditon where the 
      // difference is greater than 1
      if (Math.abs(digit2 - digit1) > 1)
         return false;

      n = parseInt(n / 10, 10);
   }

   // number checked
   return true;
}

// lets check 6789
let n = 6789;
if (checkJumbled(n))
   console.log("True");
else
   console.log("False");

// lets check 1247 
n = 1247;
if (checkJumbled(n))
   console.log("True");
else
   console.log("False");

Output:-

True
False

The number 6789 is a Jumbled number because neighboring digits deviate by at least one value whereas the number 1247 is not a Jumbled number because neighboring digits are not deviated by at least one value. Also see:- JavaScript Remove Last Digit from Number

This brings us to the end of this article. We hope you learned how to write a program for jumbled numbers JavaScript.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.