I know that FCC wants us to do a loop, but that's boring since' I've already done a type of problem like this at least twice in Edabit and Codewars. After solving the problem there I was able to review how other folks solved this problem and recall that a few have used a formula, so cue up Google to search around. Gauss on Arithmetic Sequences hit the spot! The site goes into length about the formula and why/how it works. But I found what I was looking for here:
Where n is the number of integers.
Or for the purpose of applying it to the problem, I will use the below simplified formula.
And below are two versions of the answer. (Equations created with CodeCogs)
function sumAll(arr) {
let min = Math.min(...arr);
let max = Math.max(...arr);
return ((max - min + 1)/2)*(min + max)
}
const sumAllCodeGolf = (arr) => ((Math.max(...arr) - Math.min(...arr) + 1)/2)*(Math.min(...arr) + Math.max(...arr));
Easy peasy! I think it's still good to practice writing out the loop as well. However, I am at the point where I've done enough of these arithmetic challenges that I'm getting clued into the fact that there is likely a formula for most things. If it doesn't break anything in JS, why not use it?
Quick Retrospective
Since it is Day 85 and I am 15 days away, I figured this was in order.I know it is not recommended to do this, but compared to other folks that started around the same time I did, including some of those with zero HTML+CSS experience, I feel like I am about a month or two behind them in terms of progress. If we're using FCC as a yardstick, they're all in and around the Data Visualization section and I am still stuck in the Algorithm and Data Structures.
However! I am aware that the working like a Japanese salaryman hours that my day job demanded ate up 2.5 weeks in May and then getting into the funk due to the stolen phone/invasion of privacy last month severely demotivated me for another 2 weeks or so. I also took a long detour into Udemy courses (which I still plan on finishing) as well so FCC is not the best yardstick really. I guess that works out to be why I am "a month behind."
We all know the reasons why you should not compare your current self with someone else. Everyone learns differently and may have other circumstances in their lives that may help or hinder progress. Someone spending time between semesters at their parents home, covering their room and board will probably have more time to learn than I would since I have that day job eating up 9+ hours of my weekday (if you add in commute time). And I am probably better off in terms of being able to dedicate time to learning than a working parent with kids (and kudos to them!).
But the reason I follow these folks is not only for the camaraderie, but because I find myself feeling pretty hopeless and demotivated and I actually feel like the comparisons actually help motivate me! What it boils down to is that they are able to dedicate a whole lot more hours then I can (whatever their circumstances) whereas I sometimes only squeak by with the minimum 1 hour a day (or less on a handful of days). It makes me want to find ways to get in more hours! In fact, this was the reason I got out of my funk from last month.
Also seeing someone else's progress is extremely gratifying to me as well. The thought of "I can get there too as long as I put in the time!" is the first thing that crosses my mind.
So as long as you are aware that this is not a race, I don't think comparisons are inherently a bad thing. There are always ways to turn it on its head and use it to your advantage.
No comments:
Post a Comment