Jump to content

too much recursion


hisoka

Recommended Posts

When running this script :

 

var upper =10000000;
var randomNumber = getRandomNumber(upper);
var guess;
var attempts = 0;

function getRandomNumber(upper)
{

while (guess !== randomNumber)
{
guess = getRandomNumber(upper);
attempts +=1;
if (guess == randomNumber)
{
break;
}
}
return Math.floor( Math.random() * upper ) + 1;
}

var m = getRandomNumber(upper) ;
console.log(m);

 

I got an error that says " too much recursion " !!!!!! what is the meaning of this error ?? and how to correct it ? :(

Link to comment
Share on other sites

It means that the browser has a limit on how many times a function can call itself, and you hit that limit. It's to protect the browser against your code using all of the resources on the system.

 

That code is going to cause infinite recursion, it's never going to quit. The first time you call the function it's going to return a random number, and the second time you call it it's going to have infinite recursion. Step through the code yourself to try and figure out why.

Link to comment
Share on other sites

"Step through the code yourself to try and figure out why."

 

Justsomeguy , I told you that I am very weak in programming , a newbie . I cannot understand most of the script that I posted here ,which they are not mine , let alone to understand the source of error in them . I try to run them to see what they do and according to that , I get an understanding about them . For me they are very complicated and I cannot figure out what they do even if I go through them line by line . Therefore , I post them here for clarification .. I know all the basics of javascript and php . Now , I believe that if I take a code or a script , for example , and try to study it and then take another and study it with your clarification and by time , I will be able to become very good in programming . For me it is more important to be able to understand what the most complicated code does then being able to write a code myself like a professional programmer . May be I am wrong in the method I use to learn how to understand a code very well even if it is complicated . So I would like you Justsomeguy to tell me , as my teacher , from where should I begin , besides writing a good code , in order to be able to understand , very easily , what a complicated code does ? from where ? what is the beginning ? I am lost

Link to comment
Share on other sites

I would recommend reading the W3Schools Javascript tutorial carefully. All of it is explained in detail in the tutorial. If you did not understand it the first time, read it again.

 

I don't know where you found that script, but it's completely wrong in every way.

  • Like 1
Link to comment
Share on other sites

You need to read about programming theory in general, there are several good books about it. You need to be able to read code and understand what it does without having to run it. You can run it and test it when you're starting out, but ultimately you have to be able to understand what you're reading.

 

Here's that code again, with some indenting to make it a little more readable. You should get in the habit of formatting code so that it's readable, and post code using code tags to keep the formatting.

 

var upper =10000000;
var randomNumber = getRandomNumber(upper);
var guess;
var attempts = 0;

function getRandomNumber(upper)
{
  while (guess !== randomNumber)
  {
    guess = getRandomNumber(upper);
    attempts +=1;
    if (guess == randomNumber)
    {
      break;
    }
  }
  return Math.floor( Math.random() * upper ) + 1;
}

var m = getRandomNumber(upper) ;
console.log(m);
When you tell the browser to execute that code the first thing it does is define that function to make it available if something uses it, and then it starts at the first line. So the variable upper gets the value 10000000. When you go through code yourself you should keep a list of variables and their values, because that's what the computer does. So after that first line this is the only variable defined:

 

upper                 10000000
Then, on the next line it calls the getRandomNumber function and passes the value 10000000 to it. So, you go down to the first line of that function. The first line is a while loop that says to loop as long as guess does not equal randomNumber. At this point neither guess nor randomNumber have been defined, only upper has been defined, so the values of both of them are the value "undefined". Since guess equals randomNumber (undefined equals undefined) then the while loop does not run and you skip to the next statement after it, which is a return statement where it returns a number between 1 and 10000000. Going back up to the second line where you called getRandomNumber, that value gets returned and saved as randomNumber. So now that variable has a value also:

 

upper                 10000000
randomNumber          1 <= x <= 1000000
You write this stuff down, you keep track of which variables have which values. That's how you step through code. Then you go to the next line, where it declares guess. It doesn't give guess a value, so you can add that to the list but give it the value undefined, and then it declares attempts and gives it the value 0:

 

upper                 10000000
randomNumber          1 <= x <= 1000000
guess                 undefined
attempts              0
Now you go to the next line to execute, which is after the function definition, where they try to define m. They call getRandomNumber again and pass upper, which still has the same value. So, you go back into the function. Again, the first line is the while loop, where it loops as long as guess does not equal randomNumber. randomNumber has some random value in it, and guess is undefined (as you can see from the list of variables and values above), so they aren't equal to each other. So it goes into the loop, and the first line of the loop calls getRandomNumber and passes upper to it. The value of guess hasn't changed yet, because it has to execute getRandomNumber first to figure out what the value is. So, it checks the condition in the while loop again, which is the same, guess is undefined and randomNumber is still the same random number. So, it goes into the loop. The first line in the loop calls getRandomNumber. That repeats over and over until it reaches the browser's recursion limit. After that function gets called a second time it never returns a value because it keeps calling itself because guess never has a value other than undefined.

 

That's how you read and step through code. If you can't do that by reading the code then you need to read the books about programming theory so that you can understand what the code is doing. You need to understand what loops are and how they work, how other control structures like if or switch statements work, how functions work, how variables work, etc. These are not language-specific topics, they apply to most programming languages.

  • Like 2
Link to comment
Share on other sites

"I don't know where you found that script, but it's completely wrong in every way."

 

Thank you Foxy Mod and you are totally right it is totally wrong and a nonsense

 

"If you can't do that by reading the code then you need to read the books about programming theory so that you can understand what the code is doing"

 

In the past I believed that I am able to understand what the code does by reading but I come to the conclusion that I was cheating myself . Except for easy code , I cannot understand the complicated code even when I run it many time let alone by reading it . So I am not ready . Therefore ,

You are my teacher Justsomeguy so I will do all what you tell me so , please , give me all the books you see that they are useful for me and I will read them and try to understand them but I hope you take in consideration my very weak level so you do not give me complicated books :)

 

please list them here so that I will download them from the internet and read them ( please no sponsored books as I cannot pay to have them )

Edited by hisoka
Link to comment
Share on other sites

Justsomeguy , should I begin by reading books about programming as a concept in general or I should begin by reading books about javascript as programming language for beginners ?

Edited by hisoka
Link to comment
Share on other sites

A beginning programming book would probably be more instructive than a beginning Javascript book. Books about specific languages sometimes assume you already know something about programming, or they teach you how that particular language works as if all languages work that way.

Link to comment
Share on other sites

"A beginning programming book would probably be more instructive than a beginning Javascript book"

 

So I begun to read about programming in general and those are the books I begun with :

 

Beginning Programming For Dummies Wallace Wang
Programming for the Absolute Beginner Jerry Lee Jr. Ford

Computer Programming for Kids and Other Beginners Warren Sande

 

Beginning Programming For Dummies is about Basic as a programming language

Programming for the Absolute Beginner Jerry Lee Jr. Ford is about Liberty Basic in its majority and some other programming languages

 

Computer Programming for Kids and Other Beginners is about Python

 

and the majority of the books follow the same methodology like the three books above . Therefore there is not such a book that talks about programming as a general but even these books they teach one language . Thus , I see absolutely no reason to learn Basic because if I do so , would not be more wiser to learn javascript ?!! plus Basic, as I see it in the two books , is in no way easier than javascript . So why should I lose time in learning it if I can begin with javascript and the same for python . So Justsomeguy such a book as you suggest it does not exist . my aim is to learn php and javascript . This is what I want . Is not better to begin with php and javascript books for kids and beginners !!! especially that I have some little background about both languages?????

Link to comment
Share on other sites

I second JSG's recommendation in getting a better understanding of programming, patterns, design, etc from a high level view. Most of the books I have read tend to focus on Java, however that is merely based on Java being fairly ubiquitous and most web languages have similar constructs (but different syntax). However, what i take away is how things are built, and the concepts such a DRY and SOLID transcend programming languages.

 

That said, if you are looking at programming to be more of a casual endeavor, then a JavaScript book may be all you need. If you are looking to learn backend and frontend and everything in between, a high level understanding of computer programming is recommended, in addition to books on the specific languages you are looking to pursue.

Link to comment
Share on other sites

The problem is that I did not find books that talk about programming in its high level of view . The books that I found until now seem to deal with programming in its high or general level of view but , in reality , when opened and studied in details , they deal only with one specific programming language like (Programming for the Absolute Beginner Jerry Lee Jr. Ford) . Moreover , some books deal with many languages in the same time with reference to one language more than the others but you need to have a background about all these languages in order to understand the code written in them (Beginning Programming For Dummies Wallace Wang) therefore you need to learn the basic of all these languages before reading the books!!! . Due to all that , could you please provide me with one or two books that deal with programming in its high level of view so that I can confine myself and have a concrete example of what should I read exactly as a newbie who is in the beginning of the road??

Edited by hisoka
Link to comment
Share on other sites

The vast majority of books use some language to illustrate what they're talking about. BASIC was specifically created for teaching programming, for example. When you are learning how to program you don't just read stuff, you actually do it. So you need to use some language to do that, obviously. If you're not practicing what you're learning then you're not learning it very well.

 

therefore you need to learn the basic of all these languages before reading the books!!!

No, not if the book is teaching correctly. Many books will use different languages to illustrate different things they are teaching, where they will show you what they are trying to get you to learn. You don't have to learn the entire language for those examples to make sense. My college courses looked at over a dozen languages, for example, but I didn't learn everything about all of them. They were used to illustrate specific things in programming.

 

I mean, surely you don't think that a book called "Beginning Programming For Dummies" requires you to already know several programming languages in order to understand the book.

 

I wouldn't really recommend Javascript as an intro language just because it's fairly unique, but there are several intro books for that as well.

 

https://www.quora.com/Whats-the-best-book-for-learning-to-program

Link to comment
Share on other sites

These are some of my favorites, that I have taken a lot of inspiration from

 

Note that the first 3 don't reference JavaScript at all, but I consider them to have been more helpful than the last two because it taught me more about programming at a higher level, even though I don't really code in Java (primarily JavaScript and PHP). This has made me a better developer overall because I can recognize when something feels right, versus when it doesn't (code smell), while the last two just helped me get better at only 1 language.

Link to comment
Share on other sites

First I want to say thank you for the list of books you provide me :)

 

I opened the first book in the list which is " clean code : A handbook for agile software craftsmanship for Robert . C. Martin and begun to read it until I reached a paragraph in which the author try to improve a code readability : He begun by asking a question :

 

"What is the purpose of this code?
public List<int[]> getThem() {
List<int[]> list1 = new ArrayList<int[]>();
for (int[] x : theList)
if (x[0] == 4)
list1.add(x);
return list1;
}
Why is it hard to tell what this code is doing? ..................................until

 

public List<int[]> getFlaggedCells() {
List<int[]> flaggedCells = new ArrayList<int[]>();
for (int[] cell : gameBoard)
if (cell[sTATUS_VALUE] == FLAGGED)
flaggedCells.add(cell);
return flaggedCells;
}
Notice that the simplicity of the code has not changed. It still has exactly the same number
of operators and constants, with exactly the same number of nesting levels. But the code
has become much more explicit.

We can go further and write a simple" . Neither I understood what the first code does nor did I understand what the second does . I have a question : when I face such code , while I am reading these books , and I do not understand it what should I do ? should I skip it and continue reading or should I understand what such a code does ? as a newbie who is in the beginning of the road and know nothing of programming is it necessary and obligatory , when facing those complicated sets of code while reading these books , to understand what they are doing? or I can skip them ? and if I skip them , does it impede me from improving or it does not?

Edited by hisoka
Link to comment
Share on other sites

Neither I understood what the first code does

That's part of the point he's trying to make, it's not that easy to figure out. Asking what it does is a rhetorical question.

 

when I face such code , while I am reading these books , and I do not understand it what should I do ?

Keep reading, and try to figure out what the point is. For example, if you're paying attention you would notice that those 2 pieces of code are the exact same with only different function and variable names, and the second one uses constants instead of hard-coded "magic numbers". The point he is trying to make is that code is easier to read and understand when the function and variable names make sense. That is the only point he's trying to make. You could read through the second piece of code and make a guess about what it's doing based only on the identifier names. He's trying to teach how a programmer should write code, in that lesson it is less important about what the code does than how it is written.

 

public List<int[]> getThem() {

List<int[]> list1 = new ArrayList<int[]>();

for (int[] x : theList)

if (x[0] == 4)

list1.add(x);

return list1;

}

 

public List<int[]> getFlaggedCells() {

List<int[]> flaggedCells = new ArrayList<int[]>();

for (int[] cell : gameBoard)

if (cell[STATUS_VALUE] == FLAGGED)

flaggedCells.add(cell);

return flaggedCells;

}

Link to comment
Share on other sites

Exactly, often times the biggest challenge to writing code is how well you actually express the code through "human" language. The second example favors using proper names for variables so you can infer what the code does now, and in six months from now when you or someone else has to return to it. It should be like reading a box, the code should speak for itself. Those are the lesson I try and take away, not the actual language syntax.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...