2/14. "InternalError: too much recursion", when puting string instead of a number


#1

My code works if I put any number (so I pass the lesson), but if I want to see, what happens if I enter string, error appears: InternalError: too much recursion

Can someone explain me that?

Thanks.


#4

Basically the first thing your program did was divide a string by a number. This is of course not possible but rather than breaking off the program the program kept trying(thus the recursion). This may be due to how codeacademy works but I digress.

If you want this code to work properly you're first going to have to handle whether or not you're dealing with a number.

if(isNan(number)){ 
    if(number %2 === 0) {
        return true;
    } else {
    return false;    
    }
else { return "It is not a number. Check what you are typing!"; }

Programs will always try to execute your code, they WILL attempt to divide strings by numbers if you tell them to and they WILL fail miserably at doing so! Make sure that the order in which your program is made allows for you to deal with errors before they become errors.


#5

@darthvaderkrytonite
try copy pasting this and see if it works..

var N="string";
var isEven = function(number) {
  // Your code goes here!
  if(number%2===0)
  {
      return true;
  }
  else if(isNaN(number))
  {
      return "this isn't a number";
  }
  else
  {
      return false;
  }
};
isEven(N);


#6

Could you post the acutal code that one could run it? From what I see there should not be any recursion.
PS: Just in case you wonder I deleted the posts about the N as this missunderstanding is not relevant to the topic, is it?

@darthvaderkrytonite did you named your isEven function isNaN in a former try?


#7

A good point, but I cannot see how this is related to a recursion (calling a function inside itself).


#8

This is the code:

var N = "string";
var isEven = function(number) {

if(number %2 === 0) {
  return true;
} else if (isNaN(number)){
  return "It is not a number. Check what you are typing";
} else {
   return false;   
  }

};
isEven(N);

Its funny, now it's working. I hope i am not going crazy.


#9

Ok as said I'm more suprised about it throwing the recursion error. :smile:
In case you think you have a correct code you may also try to refresh the page. Often enough former fails put garbage in the memory that effects later tries and refreshing gets rid of this by resetting everything in the memory. Of course this cannot fix real errors.


#10

Thanks haxor789 for looking into it. Good point about the garbage in the memory, happened to me also before.


#11

Could someone please explain why var N="string"; is necessary (or what that even means)? My code did not include that and worked.


#12

I was wondering if you could tell me how you screenshot your code into the Q&A forum?


#13

My understanding is the word "string" is what is going to be tested, so in this case because the word "string" is not a number then it brings up the error message "This is not a number" or however you phrased it. If N = "1" or "2" then it would have returned false for 1 and true for 2. Using var N allows you to pass something through your code.