- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Cutting off number at each digit to construct an array in JavaScript
Problem
We are required to write a JavaScript function that takes in a number. Our function should return an array of strings containing the number cut off at each digit.
Example
Following is the code −
const num = 246; const cutOffEach = (num = 1) => { const str = String(num); const res = []; let temp = ''; for(let i = 0; i < str.length; i++){ const el = str[i]; temp += el; res.push(temp); }; return res; }; console.log(cutOffEach(num));
Output
Following is the console output −
[ '2', '24', '246' ]
Advertisements