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

  • Home
  • SEARCH
  • 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 8314309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:46:27+00:00 2026-06-08T20:46:27+00:00

Working on an assignment for my Intro Programming class, I created a function and

  • 0

Working on an assignment for my Intro Programming class, I created a function and got everything working with a little help from some guys on here (thanks a ton btw) and now I have a new issue. I have two variables that I declare and prompted for a value before starting my function and causing it to prompt for a new value if it doesn’t pass. But now when I return them it doesn’t change the variable outside of the code.

// 1 Declare Variables
var numTrees;
var counter = 0;
var answer = "no";

function treeFunction(answer, counter, numTrees) {
while (answer == "no" && counter < 3)
{
    if (numTrees < 5 || numTrees > 10)
    {
        alert("That is an incorrect value.\nThe sample size should be less than 5 or greater than 10.\nPlease try again.");
        answer = "no";
        numTrees = prompt("Please reenter the amount of trees in your sample.");
        alert("You have entered: " + numTrees)
        counter + 1;
    }
    else 
    {
        answer = "yes";
    }
}
if (answer == "no") {
    alert("You have entered an incorrect number too many times.\nThe Program will now end.");
    window.open('', '_self', '');
    window.close();
} else if (answer == "yes") {
    return numTrees;
}
}
// 2 Prompt the Instructor for the number of Trees
numTrees = prompt("How many trees are in your sample?");
alert("You have entered: " + numTrees);
treeFunction(answer, counter, numTrees)
document.write(numTrees); 
document.write("<br/>")
document.write("<br/> <br/>" + "End of Program.");

EDIT: I have change what I’m doing, I no longer need to return both variables, only numTrees. howver the document.write for some reason show the original value, not the value it was changed to in the function even after return.

  • 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-08T20:46:29+00:00Added an answer on June 8, 2026 at 8:46 pm

    answer, counter, numTrees are global, however when you pass them into a function as arguments;

    function treeFunction(answer, counter, numTrees) {
    

    you get a set of new local variables which happen to have the same name as the globals, and therefore hide them.

    To use the globals, dont pass them

    function treeFunction() {
       answer = ....
    

    (You also want counter += 1 to actually increment the variable)

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

Sidebar

Related Questions

I'm working on an assignment for my programming class but I've run into some
I am working on an assignment for my GUI programming class, in which we
Having some issues with one small function I'm working on for a homework assignment.
Working on my assignment for Java and I have created a class called Triangle.
I am working on a homework assignment for my into to programming class that
i`m working on my assignment for univ, and since some parts are not really
I am working on an assignment and don't get answer for some of questions.
I am working on an assignment that deals with reading data from a text
I'm working on an assignment has a class hierarchy like blow: abstract EventClass someEventClass1
I'm working on a homework assignment and I ran into a little snag. I'm

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.