
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding nth digit of natural numbers sequence in JavaScript
Natural Number Sequence:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12...
This sequence extended infinitely is known as natural number sequence.
We are required to write a JavaScript function that takes in a number, num, as the first and the only argument. The function should find and return the (num)th digit that will appear in this sequence when written, removing the commas and whitespaces.
For example −
If the input number is −
const num = 13;
Then the output should be −
const output = 1;
because '1234567891011' this string has its 13th number as 1
Example
The code for this will be −
const num = 13; const findDigit = (num = 1) => { let str = ''; let i = 1; while(str.length < num){ str += i; i++; }; const required = str[num - 1]; return required; }; console.log(findDigit(num));
Output
And the output in the console will be −
1
- Related Questions & Answers
- Finding the nth digit of natural numbers JavaScript
- Finding nth element of an increasing sequence using JavaScript
- Finding nth element of the Padovan sequence using JavaScript
- Finding the nth element of the lucas number sequence in JavaScript
- Finding value of a sequence for numbers in JavaScript
- Finding sequential digit numbers within a range in JavaScript
- Building a lexicographically increasing sequence of first n natural numbers in JavaScript
- Finding the nth palindrome number amongst whole numbers in JavaScript
- Finding sum of first n natural numbers in PL/SQL
- Nth Digit in C++
- Digit distance of two numbers - JavaScript
- Finding the nth prime number in JavaScript
- Finding sum of every nth element of array in JavaScript
- Finding greatest digit by recursion - JavaScript
- Finding the sum of all numbers in the nth row of an increasing triangle using JavaScript
Advertisements