
- 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
Counting specific digits used in squares of numbers using JavaScript
Problem
We are required to write a JavaScript function that takes in an integer n (n >= 0) and a digit d (0 <= d <= 9)
Our function should square all numbers k (0 <= k <= n) between 0 and n and count the numbers of digits d used in the writing of all the k**2.
Example
Following is the code −
const n = 25; const d = 1; const countDigits = (n, d) => { let k = 0, count = 0; d = d.toString(); while (k <= n) { let a = 0; let s = (k*k).toString(); for(let i = 0; i < s.length; i++) if(s[i] == d) a++; if (a > 0) { count += a; }; k++; }; return count; }; console.log(countDigits(n, d));
Output
11
- Related Articles
- Counting n digit Numbers with all unique digits in JavaScript
- Counting smaller numbers after corresponding numbers in JavaScript
- Counting numbers after decimal point in JavaScript
- Counting the clusters of positive numbers - JavaScript Arrays
- Counting rings in letters using JavaScript
- Counting divisors of a number using JavaScript
- Counting prime numbers that reduce to 1 within a range using JavaScript
- Fetch Numbers with Even Number of Digits JavaScript
- Average of Squares of Natural Numbers?
- Store count of digits in order using JavaScript
- Counting prime numbers from 2 upto the number n JavaScript
- Sum of squares of Fibonacci numbers in C++
- What is Counting numbers ?
- Sum of perimeter of all the squares in a rectangle using JavaScript
- Counting How Many Numbers Are Smaller Than the Current Number in JavaScript

Advertisements