get every 7th digit in javascript

To get every 7th digit in an integer in JavaScript, you can follow this approach:

  1. Convert the integer to a string.
  2. Use a for loop to iterate over the string, checking whether the index is a multiple of 7.
  3. Use an if statement to extract the digit at that index.
  4. Add the extracted digit to an array.
  5. Repeat steps 2-4 until you reach the end of the string.

Here's the code for the same:

index.tsx
let num = 1234567890123456;
let numString = String(num);

let seventhDigits = [];

for(let i=0; i<numString.length; i++) {
    if((i+1)%7 === 0){
        seventhDigits.push(numString[i]);
    }
}

console.log(seventhDigits);
// Output: ["7", "4"]
247 chars
14 lines

gistlibby LogSnag