Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9302291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:13:17+00:00 2026-06-18T23:13:17+00:00

I have the following exercise code where I’m trying to calculate the sum of

  • 0

I have the following exercise code where I’m trying to calculate the sum of all the primes from 1-10 which is crashing due to recursion.
I’m having trouble with my for and while loops as the code doesn’t seem to be cycling through my var i, and is getting stuck on the initial assignment of i = 3.
I’m not looking for the correct answer or the most efficient answer yet but I am looking for someone to help me understand what’s wrong.
Here’s the code:

var array = [2];
var total = 0; 
var j = 0;
function isPrime(i, j) {
if ( i%array[j] === 0 ) {
        console.log("i was " + i + " and j was " + j);
        console.log(i + " is not a prime");
        j = array.length;
    }
    else if ((j + 1) === array.length) {
        console.log(i + " is a prime");
        total += i;
        console.log("total so far is " + total);
        array.push(i);
        console.log(array);
        j = array.length;
        console.log(j);
    }
    else {
        j++;
        isPrime(i,j);
    }
}

for(var i = 3; i <=10; i++) {
   while(j < array.length) {
       console.log("i is " + i + " and j is " +j);
       isPrime(i, j);
   }
}

console.log(total);
console.log(array);  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:13:18+00:00Added an answer on June 18, 2026 at 11:13 pm
    var j = 0;
    function isPrime(i, j) { …
    

    means that you have two distinct j variables: One outside the function, and one inside that shadows the other. From inside you never will be able to assign or read the outer variable. Therefore, the outer j stays 0 forever and while (j < array.length) will loop infinitely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code like this, which is solved by me following a exercise
I have the following simple code from an exercise in Modern Javascript used to
I have the following code, copied exactly from a exercise from a PHP book.
I have the exercise with following code int FindFirstSet(unsigned BitMap, unsigned start) { unsigned
I have the following code below which seems to be working fine: // GPS
I have the following code, where i am trying to create a generic collection
I have the following code: $bind = new COM(LDAP://CN=GroupName,OU=Groups,OU=Division,DC=company,DC=local); When I execute it from
I have the following Perl code, which reads the input and indents the file
I'm trying to make the following exercise from Thinking in C++ Vol 2, witch
i have the following code which will displays result in a UItable view along

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.