Has anyone else ever experienced this? I've been learning javascript and using variables with all different sorts of names, but when I went to HackerRank and other places, I see that the variable being used is called "n". I don't know about anyone else, but that looks way too much like algebra to me, which induced a sort of panic reaction in me.
I was never very good at math but I've read in several places that you really only use basic math in coding anyway. Math is the reason I didn't minor in Computer Science like I wanted to over 10 years ago when I was in college. Back then, I just assumed that computer science included lots of math and I wouldn't do well so I didn't bother. Now I'm kicking myself for that decision.
Regardless, does anyone else freeze up at seeing the variable "n" being solved for? It takes me back to those horrible high school math classes. Why is it done this way?
n refers to the nth iteration of the equation.
Consider it similar to i, j or k (which are also from mathematics fyi). It's just a way for us to identify when a specific iteration has met the criteria of the equation and then process that specific, nth, iteration accordingly.
I truly don't understand we you reduce to panic in these situations - Unless you're new to programming? In which case you will find many examples online of such variable naming conventions - especially in computationally intensive calculations. Such as finite element method or computational fluid dynamics.
I was never very good at math but I've read in several places that you really only use basic math in coding anyway. Math is the reason I didn't minor in Computer Science like I wanted to over 10 years ago when I was in college.
The feels. :( I wished I studied CS too but the idea of maths put me off.
It's common to see 'n' used as the number of things -- especially in a loop. for instance:
const n = someCalculation();
for (let i = 0; i < n; i++) // ...
The only other place I'd use 'n' rather than something more descriptive would be in a lambda such as:
const neighbourNames = neighbours.map(n => n.name);
As for math... you're right that programming typically only involves basic math. The kind of math that matters for Computer Science is for things like analyzing run time of functions so you can write performant algorithms.
Andrew Wooldridge
eBay WebDev
One idea might be to take a night class at a local college and take control of that fear. Mastery destroys fear, and you'll be a better developer because of it.