Home  >  Article  >  Web Front-end  >  JavaScript program to calculate number of spins divisible by 4

JavaScript program to calculate number of spins divisible by 4

WBOY
WBOYforward
2023-08-25 18:09:02670browse

JavaScript 程序计算可被 4 整除的旋转次数

In this tutorial, we will learn to calculate the total number of spins for a given number that is divisible by 4.

Problem Statement - We are given a numerical value. We need to rotate the number clockwise or counterclockwise and count the total number of rotations that are divisible by 4.

Here we will learn two different ways to calculate the number of rotations divisible by 4.

Rotate the number and check if it is divisible by 4

In this method, we first convert the number into a string. We can perform n rotations on a string of length n. We will remove the first character of the string and add it to the last character of the string. Afterwards, we can check if the new number generated by the rotation is divisible by 4.

grammar

Users can follow the following syntax to check whether the rotation is divisible by 4 and rotate the numeric string.

for ( ) {
   if (parseInt(numStr) % 4 == 0) {
      count++;
   }
   numStr = numStr.substring(1, len) + numStr[0];
}

In the above syntax, the parseInt() method is used to convert the string into a number, and the substring() method is used to rotate the string.

algorithm

  • Step 1 - Use toString() method and convert the number to string.

  • Step 2 - Use a for loop to perform a total of "n" rotations on a string of length "n".

  • Step 3 - Convert the string to a number using parseInt() method and check if the number is divisible by 4. If the number is divisible by 4, the count variable is incremented and decremented by 1.

  • Step 4 - Use the substring() method to get the substring from the first index. Additionally, the first character of the string is appended to the end of the substring. This way, we can rotate the string and generate a new number.

Example 1

In the example below, we define the countRotations() function, which implements the above algorithm and returns the total number of rotations divisible by 4. In the output, the user can observe that the total number of spins of the number is divisible by 4.

<html>
<body>
   <h3> Program to find the total number of rotations divisible by 4 </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      // JavaScript program to find the total count of rotations divisible by 4
      let countRotations = (number) => {
         let numStr = number.toString();
         let len = numStr.length;
         let count = 0;
         
         // Loop to traverse the string
         for (let i = 0; i < len; i++) {
         
            // Check if the string is divisible by 4
            if (parseInt(numStr) % 4 == 0) {
               count++;
            }
            
            // Rotate the string
            numStr = numStr.substring(1, len) + numStr[0];
         }
         return count;
      }
      let number = 121342435345;
      output.innerHTML = "Total count of rotations divisible by 4 of " + number + " is " + countRotations(number);
   </script>
</body>
</html>

Check whether each pair of 2-digit numbers is divisible by 4

If the last 2 digits of any number are divisible by 4, we can say that number is divisible by 4. When rotating numbers, each pair of two digits appears at the end of the number. Therefore, we can check whether any pair of two numbers is divisible by 4; we can say that one rotation associated with that pair is divisible by 4.

grammar

Users can follow the syntax below to extract a pair of two-digit numbers from a number and check whether it is divisible by 4.

let lastDigit = num % 10;
num = Math.floor(num / 10);
let secondLastDigit = num % 10;
if ((secondLastDigit * 10 + lastDigit) % 4 == 0) {
   count++;
}

In the above syntax, we get the last and penultimate digit from the number. After that, we create a two-digit number using both and check if it is divisible by 4. If so, we increment the count variable.

algorithm

  • Step 1 - If the number is single digit, check if it is divisible by 4. Returns 1 if yes; otherwise returns 1. Otherwise return 0.

  • Step 2 - If the number contains two or more digits, initialize the "count" variable to 0.

  • Step 3 - Now, we need to create a pair using the last digit and the first digit of the number. Use the modulo operator to get the last digit and the Math.log() method to get the first digit.

  • Step 4 - Multiply the last digit by 10 and then multiply the first digit by 10. Then check if the result is divisible by 4. If it is divisible by 4, add 1 to the count.

  • Step 5 - Use a while loop to check the other two number pairs. In the while loop, use the modulo operator to get the last and second-to-last number. Create a pair using two numbers and check whether the pair is divisible by 2. If so, increase the count by 1.

Example 2

In this example, the countRotations() function counts the number of two-digit pairs that are divisible by 4. It implements the above algorithm and returns a count value after all operations are completed.

<html>
<body>
   <h3> Program to find the total number of rotations divisible by 4 </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      function countRotations(number) {
         //If the length of the number is equal to 1, check if the digit is a multiple of 4
         if (number < 10) {
            return number % 4 == 0 ? 1 : 0;
         } else {
            // Initialize count of rotations divisible by 4
            let count = 0;
            let num = number;
            //Check for the last digit and the first digit
            let lastDigit = number % 10;
            // Get the first digit from the number
            let firstDigit = Math.floor(number / Math.pow(10, Math.floor(Math.log10(number))));
            //If the last digit and first digit are divisible by 4, then add 1 to count
            if ((lastDigit * 10 + firstDigit) % 4 == 0) {
               count++;
            }
            while (num > 0) {
               // get last digit of number
               let lastDigit = num % 10;
               // get second last digit of number
               num = Math.floor(num / 10);
               let secondLastDigit = num % 10;
               if ((secondLastDigit * 10 + lastDigit) % 4 == 0) {
                  count++;
               }
            }
            return count;
         }
      }
      let number = 90645232432;
      output.innerHTML = "Total count of rotations divisible by 4 of " + number + " is " + countRotations(number);
   </script>
</body>
</html>

The user learned to find the total number of spins for a number divisible by 4. We see two different approaches. The first method converts the number to a string, rotates the string, converts the string to a number again, and checks whether the newly generated rotation is divisible by 4.

The second method counts the total number of two-digit pairs that are divisible by 4.

The above is the detailed content of JavaScript program to calculate number of spins divisible by 4. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete