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 7776529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:54:29+00:00 2026-06-01T17:54:29+00:00

I have a loop for a coin flipping program that I’m making. the problem

  • 0

I have a loop for a coin flipping program that I’m making. the problem is the it seems to be exiting early. Take a look.

$(function() {
    $('#rollDice').click(function() {

        var e = document.getElementById("diceSides");
        var diceSides = e.options[e.selectedIndex].text;
        var diceRolls = document.getElementById('rollCount').value;

        if (diceRolls.match(/^[\d]*$/ )) {      
            if (diceRolls == "")  {
                alert ("Please fill out all forms then try again.");

            } else {

                $('#diceRollContainer').slideDown('slow');

                for (i=0;i<diceRolls;i++) {
                    var randNum = Math.floor(Math.random()*diceSides)+1;
                    var rolls = ("You rolled a " + diceSides + " sided die " + diceRolls + " times, and got the numbers ");

                    rollMinOne = rolls - 1;
                    if (i == rollMinOne) {
                        var rolls = (rolls + randNum + ".");
                    }
                    var rolls = (rolls + randNum + ", ");

                }
                alert (rolls);
            }
        } else {
            alert ("Make sure you only enter numbers and no spaces, then try again.");
        }
    });
});

The problem is that the program is alerting rolls before the for loop seems to be completed. Why is it doing this?

  • 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-01T17:54:32+00:00Added an answer on June 1, 2026 at 5:54 pm

    You have several bugs in that code, but the one that explains the behavior you’re seeing is that you reset the value of rolls every time through the loop to the initial string.

    Once you move that line out and you get a closer value, but you are also computing rollsMinOne from rolls, rather than diceRolls, as you intended (this is why choosing good names is so important), meaning that if statement is never true (since a string minus a number is the value NaN “Not a Number”, which is not equal to anything [even itself!]).

    Then the only functional (rather than style or design) problem is that you are then appending a value with a comma on the end even if you already added it with a period.

    Putting it all together:

        var rolls = ("You rolled a " + diceSides + " sided die " + diceRolls + " times, and got the numbers ");
        for (i=0;i<diceRolls;i++) {
            var randNum = Math.floor(Math.random()*diceSides)+1;
    
            rollMinOne = diceRolls - 1;
            if (i == rollMinOne) {
                rolls = (rolls + randNum + ".");
            } else {
                rolls = (rolls + randNum + ", ");
            }
    

    Though as the other answers mention, there are easier and faster ways to get the same result, I feel it’s important to understand why code doesn’t work.

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

Sidebar

Related Questions

The problem with octave(matlab). In the program I have loop where I plot data.
I have a loop that finds duplicate lines in a .ini file. I can
I have a loop that runs through a variety of websites and I'd like
I have a loop created with each, check this example: $('.foo').each(function(i){ //do stuff });
I have the loop below: var myArray = []; $(this).children('a').each(function () { myArray.push($(this).attr('href')); });
I have a loop that looks something like this while(condition){ read_some_data(source, buf, BUFSIZE); printf(buf);
I have a loop that is creating a drop down of 31 days. days
I have a loop in my C++/OpenMP code that looks like this: #pragma omp
I have a loop in my KornShell (ksh) script that I want to execute
I have while loop and in that there's forum posts.I want to show number

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.