We are going to write a program in JavaScript that counts the number of rotations of a given number that is divisible by 10. We will loop through the number of spins of the number and check if each spin is divisible by 10. If the rotation is divisible, we will increment our count. Finally, we will return the count as the result of the program. This program is useful in various applications as it provides a simple solution to check whether a number is divisible by 10.
The way to solve this problem is as follows -
Initialize a counter variable to store the number of rotations.
Loop over the given array of numbers and generate all possible rotations.
For each rotation, convert the array into a single number by concatenating the array elements.
Check if the number is divisible by 10, if so, increment the counter.
Repeat steps 2-4 until all possible rotations have been checked.
Return counter as result.
This is an example of a JavaScript program to calculate the number of rotations divisible by 10 -
function countRotations(arr) { let count = 0; for (let i = 0; i < arr.length; i++) { let rotated = arr.slice(i).concat(arr.slice(0, i)); let rotatedString = rotated.join(""); if (Number(rotatedString) % 10 === 0) { count++; } } return count; } const arr = [50, 20, 100, 10]; console.log(countRotations(arr));
countRotations The function takes the array arr as input.
Variable count is initialized to track the number of spins divisible by 10.
for Loop iterates over the elements of arr.
On each iteration, rotate the array by taking a slice of arr starting from the current index to the end and arr starting from the beginning to current index.
rotatedString is created by joining the elements of the rotated array using the join method.
李> Theif statement checks whether the number represented by rotatedString is divisible by 10. If so, count is incremented.
Finally, the count is returned as the result.
In the example, the input array arr is [50, 20, 100, 10], and the output is 4 because the array has 3 times Rotation, divisible by 10: 10010, 01001, 10001.
The above is the detailed content of JavaScript program to calculate number of spins divisible by 10. For more information, please follow other related articles on the PHP Chinese website!