Breaking the system, No safety mechanism built in to codecademy?


#1

Here is some code
var name = prompt("Please enter your name below","here");

while(name!=NaN||name===undefined)
{
name = prompt("Please enter your name below","here");
}
Don't run this code, since it is an infinte loop. It will will crash your browser.

How do I know? I ran it, first the codecademy tab froze so I couldn't reset the code. Soon the page became unresponsive and I had to use task manager to end my chrome browser.

So my question is, why hasn't codecademy built in a system which prevents something like this from happening? After all, this is is a place to learn programming, and I am sure someone newbies (like me) have run into this problem many times before. Myabe termination after a set time?


A good article, definition, or discussion that explains RuntimeError: maximum recursion depth exceeded
#2

Sometimes this DOES happen. It depends on the course. Some of the Learn courses, Learn PHP, Learn JS, Learn Java, etc., will end after about 5 to 10 seconds. I think this has been something they've thought about, but they're focussing on the new course now, so you'll probably see a lot more of this if you do Learn Python or Learn Ruby.


#3

The problem is that your code is creating an infinite loop which is very dangerous because this may crash your browser or even your computer so be careful when creating loops. :slight_smile:


#4

Very true, I didn't bring this up, but infinite loops are definitely a user error. You just have to think true it carefully, especially since you know that infinite loops are possible. Pseudo code helps a lot with this because you can see exactly what you are saying. The other part of this is that you're very lucky that you just have to refresh your browser. If you're using an IDE on your computer, it could be a lot worse.


#5

I've had my fair share with infinite loops its quite spine-chilling :sweat_smile:


#6

Indeed it is! Most of mine thankfully have been with codecademy, but I had one the other day in jGRASP using Java, and I was freaking out because I had no idea what to do! Anyways, it is interesting that this is such a problem in the coding community, but the more advanced you get, the less you hear about it honestly. You just start remembering how to avoid them.


#7

Yes yes, with more experience this will happen less. but we are talking about a code learning website. Where I bet my worth that inexperienced learners will come to. So the question is why hasn't codecademy put in any safety features?


#8

There is only safety feature for infinite loops and it is when the console reaches its maximum recursion depth and codecademy's text editor and any text editor or interpreter for that matter has no "in-built, automatic"safety features because infinite loops cannot be controlled that is why to avoid infinite loops, it is JS best practice to use break statements or do..while loops. :slight_smile:

Other than that when your code has a loop go through before pressing run otherwise for example if you are n an online interpreter and you create an infinite loop your laptop could crash! :cry:


#9

So is their no way to programm in a failsafe swithc? After a certain amount of iterations couldn't the code stop executing?


#10

That is what Maximum Recursion Depth is, but it is never a good error to receive because this means that the console or shell's dynamic memory has been overloaded to the point where it cannot continue to run. This is a danger bith to your console and computer.


#11

I just found something, turns out Chrome has a failsafe switch built in. After a certain amount of time in (s) the browser will prompt the user, to either kill tab or wait. . So the best browser to run online code interpretator would be chrome. I dont think any other browsers do this.

I just tried it. No need to open up task manager. Chrome will kill the tab, is that what you mean by maximum recursion depth?


#12

Maximum recession depth is like saying, "when it has done this thing ___ many times, this is the maximum it's allowed to do, so now it will stop." The reason this is not a good idea though, is because programming is very often used to calculate things that are too dangerous to do in real life. That's where the Monty Carlo method is helpful to know. If you want to calculate something simple like population statistics, and you want to run the loop 2,000,000 times, the computer will take a few minutes to do that, so if the maximum recession depth was 2,000,000, you wouldn't get the outcome of your program. I don't know a ton about this, and I definitely understand where you're coming from, but I think that it's best to learn in a browser about infinite loops before you do it to your computer.


#13

A recursive function is a function that calls itself.

The first call is made from outside the function. Then, within the function, it may call itself. That call can, of course, lead to another call, and so on.

So that the recursion does not become infinite, a base case should be defined inside the function, in which when some condition exists, the function does not call itself, and the recursion ends.

Following is an example of a recursion in Python 3 If the function is called with an n that is less than 1, the recursion will be infinite.

def sum_of_one_to_n(n):
    """ Returns the sum of all integers from 1 to n, inclusive."""
    # Call this function with an integer >= 1.
    # Otherwise, it will raise an infinite recursion (RecursionError).
    if n == 1:
        # base case
        return 1
    else:
        # recursive case
        return n + sum_of_one_to_n(n - 1)

print(sum_of_one_to_n(7))

As an exercise for the reader, the function could be enhanced a bit to prevent an infinite recursion. For example, it could refuse to process an n that is not a positive integer.


#14

roger, and thanks for the help!


#15

I dont understand python, sorry :disappointed_relieved:, but i understand what you mean. A function that calls itself without any break or stopping method is bad. So as a programmer you must add one.