
- 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
Number of letters in the counting JavaScript
We are required to write a JavaScript function that takes in a number, say n. The function should count the letters in the number names from 1 to n.
For example − If n = 5;
Then the numbers are one, two, three, four, five.And the total number of letters are 19, so the output should be 19.
Example
const sumUpto = (num = 1) => { let sum = 0; const lenHundred = 7; const lenThousand = 8; const lenPlaceOnes = [0,3,3,5,4,4,3,5,5,4]; const lenPlaceTens = [0,3,6,6,5,5,5,7,6,6]; for (let i = 1; i <= num; i++) { let placeOnes = i % 10; let placeTens = Math.floor(i / 10) % 10; let placeHundreds = Math.floor(i / 100) % 10; let placeThousands = Math.floor(i / 1000) % 10; sum += lenPlaceOnes[placeOnes]; sum += lenPlaceTens[placeTens]; if (placeHundreds != 0) { sum += lenHundred + lenPlaceOnes[placeHundreds]; } if (placeThousands != 0) { sum += lenThousand + lenPlaceOnes[placeThousands]; } if (placeTens === 1) { switch (placeOnes) { case 4: case 6: case 7: case 9: sum += 1; break; } } if (i > 100 && i % 100 != 0) { sum += 3; } } return sum; } console.log(sumUpto(12)); console.log(sumUpto(5)); console.log(sumUpto(122));
Output
This will produce the following output −
51 19 1280
- Related Articles
- Counting rings in letters using JavaScript
- Counting the number of letters that occupy their positions in the alphabets for array of strings using JavaScript
- Counting the number of 1s upto n in JavaScript
- Counting number of 9s encountered while counting up to n in JavaScript
- Counting the number of redundant characters in a string - JavaScript
- Counting number of words in a sentence in JavaScript
- Counting divisors of a number using JavaScript
- Counting number of vowels in a string with JavaScript
- Counting number of triangle sides in an array in JavaScript
- Counting steps to make number palindrome in JavaScript
- Counting the number of IP addresses present between two IP addresses in JavaScript
- Counting prime numbers from 2 upto the number n JavaScript
- Counting the number of palindromes that can be constructed from a string in JavaScript
- Counting How Many Numbers Are Smaller Than the Current Number in JavaScript
- Counting Number of Dinosaurs in Python

Advertisements