JavaScript program to calculate number of spins divisible by 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.
