Practicing Javascript


#1

Just practicing JS because I want to do some webapps in the future.

The range() function needs to return an array containing the numbers from start up to stop, and the sum() function takes an numeric array and returns the sum of it.

I’m playing with the arguments object in JS to change some behaviors, which is very cool. And today I just realized (again!) that in Python we already have something similar like *args an **kwargs, but I didn’t try it yet.

So here is the code:


and here you can check it online (if this interests you):


#2

Hmm, I think you need to watch setting default parameters of [] or other containers. IIRC JavaScript uses a reference to one array, so each time you call it that empty array continues to grow and acts like a global single array. If that’s true (as they may have fixed that or I might be wrong), then you’ll want to not set a default and then if array is nil then set it to an empty array. Also, don’t call it array. That might shadow some other thing in JS that you don’t know about.