Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
Count the number of occurrences of 7 in a number given by the user
while (a) {

        b.push(a % 10);
        a = Math.floor(a / 10);
        if (b == 7) {
            n = n + 1;
        }
        console.log("<br><br>number of 7's:" + n);

    }

This is what I have come up with. The output is one of the numbers has seven; if not, then zero. I want the program to count the number of times seven appears in a number.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can convert the number to a string, and then count how many times a character = 7:

let n = 7326577
let cnt = 0;

let strN = '' + n;

for(let c of strN)
  if(c == '7')
    cnt ++

console.log('Number of 7\'s in number: ' + cnt)

about 4 years ago · Juan Pablo Isaza Report

0

Following you approach you need to store the last digit to a different variable and use that for checking if it is a 7

var a = 709728457;
var b = [];
var n = 0;

while (a) {
  const lastDigit = a % 10;
  b.push(lastDigit); // if you still need to store all digits
  a = Math.floor(a / 10);
  if (lastDigit == 7) {
    n = n + 1;
  }
}

console.log("number of 7's:" + n);

about 4 years ago · Juan Pablo Isaza Report

0

var a = 7686774737
var no = String(a).split('').filter(e=>e==7).length;
console.log(no)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!