
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding the nth digit of natural numbers JavaScript
We know that natural numbers in Mathematics are the numbers starting from 1 and spanning infinitely.
First 15 natural numbers are −
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Therefore, the first natural digit is 1, second is 2, third is 3 and so on. But when we exceed 9, then tenth natural digit is the first digit of 10 i.e., 1 and 11th natural digit is the next i.e., 0.
We are required to write a JavaScript function that takes in a number, say n, and finds and returns the nth natural digit.
Example
const findNthDigit = (num = 1) => { let start = 1; let len = 1; let count = 9; while(num > len * count) { num -= len * count; len++; count *= 10; start *= 10; }; start += Math.floor((num-1)/len); let s = String(start); return Number(s[(num-1) % len]); }; console.log(findNthDigit(5)); console.log(findNthDigit(15)); console.log(findNthDigit(11)); console.log(findNthDigit(67));
Output
And the output in the console will be −
5 2 0 8
- Related Articles
- Finding nth digit of natural numbers sequence in JavaScript
- Finding the nth palindrome number amongst whole numbers in JavaScript
- Finding sequential digit numbers within a range in JavaScript
- Finding the sum of all numbers in the nth row of an increasing triangle using JavaScript
- Finding the nth prime number in JavaScript
- Finding nth element of the Padovan sequence using JavaScript
- Finding the nth power of array element present at nth index using JavaScript
- Finding the nth element of the lucas number sequence in JavaScript
- Finding greatest digit by recursion - JavaScript
- Finding nth element of an increasing sequence using JavaScript
- Finding the nth day from today - JavaScript (JS Date)
- Finding the nth missing number from an array JavaScript
- Finding sum of every nth element of array in JavaScript
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript
- Finding sum of first n natural numbers in PL/SQL

Advertisements