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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:05:10+00:00 2026-06-16T05:05:10+00:00

I want to create a dynamic worksheet for my students, so every time they

  • 0

I want to create a dynamic worksheet for my students, so every time they do it they see different questions. The question that I am trying to create ie, calculate the percentage if I scored X out of a total of Y.

Here are the 3 functions which work together, the first generates some numbers, calls the second, which in turn calls the third to check if it more than 2 decimal places, then if it is, the second creates a new SCORE number which repeats until it finds an answer which is 2 decimal places or less, then returns the SCORE number which works to the first, which outputs it.

I keep getting one of three outputs : undefined where the SCORE should be, no output at all, or a working question.

I cannot understand how it works sometimes, throws undefined sometimes and gives completely nothing at other times.

Any ideas.

function scorePercent()
{
    var output="";
    var total = Math.floor((Math.random()*99)+1);
var score = Math.floor((Math.random()*(total-1))+1);
    output = output + "<div>A score of " + chkScore(score,total) + " out of " + total + ".</div></br>";

    document.getElementById("qOut").innerHTML=output;

}

function chkScore(n1,n2)
{
    var answ = (n1/n2)*100;
    if(dps(answ)>2)
    {
        var scoreNew = Math.floor((Math.random()*(n2-1))+1);
        chkScore(scoreNew, n2);
    }
    else
    {
        return n1;
    }       
}

function dps(num) 
{
    var match = (''+num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
    if (!match) { return 0; }
    return Math.max(
            0,
        // Number of digits right of decimal point.
            (match[1] ? match[1].length : 0)
        // Adjust for scientific notation.
         - (match[2] ? +match[2] : 0));
}
  • 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-16T05:05:12+00:00Added an answer on June 16, 2026 at 5:05 am

    You have a recursive function in your chkScore, but you’re not return-ing the results from the “deeper” iterations.

    Try this:

    function chkScore(n1,n2){
        var answ = (n1/n2)*100;
        if(dps(answ)>2) {
            var scoreNew = Math.floor((Math.random()*(n2-1))+1);
            return chkScore(scoreNew, n2); // <-- return that.
        } else {
            return n1;
        }       
    }
    

    The missing return there, resulted in the function sometimes not returning anything.

    The “deeper” iterations will return their value only 1 “level” up, so that “level” will have to pass it through, if you know what I mean.

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

Sidebar

Related Questions

I want to create a dynamic proxy, which can delegate its methods to different
I'm trying to create a dynamic type in .Net. I want to get a
I want to create a dynamic php date format. Example: show the time of
I want to create a dynamic array that will look like this, using years:
I want to create dynamic UITextField with different object name.I have shown the code
I want to create dynamic lambda expressions so that I can filter a list
I want to create a dynamic website that does not support IIS. The area
I want to create a dynamic menu that will get it's items from a
I want to create a dynamic 2d array by function but it seems that
I want to create a dynamic Accordion, My usecase is like, On every click

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.