r/learnjavascript • u/Rheykar • Jul 12 '22
I just completed a pretty basic exercise and became unreasonably happy
17
u/dromance Jul 12 '22
Nothing unreasonable about this. All this stuff matters. Be proud of yourself and learn the basics until you can do it as fluently as you write english.
5
u/Rheykar Jul 12 '22
Thanks! Yeah, I have to try to do it regularly haha
3
u/dromance Jul 12 '22
Yeah thats my best advice. I didnt make progress for a long time. It wasnt until i started literally writing the same program, daily and consistently, that I kind of started having my ah ha moments. Now I have a solid understanding of the basics and can write my own Tic tac toe game from scratch Lol!
15
7
u/riskybusinesscdc Jul 12 '22
Way to go, young blood. Enjoy the journey!
5
u/Rheykar Jul 12 '22
Thank you!
5
u/riskybusinesscdc Jul 12 '22
Small suggestion/next step: Convert your function into an arrow function.
5
7
u/holdyrbreath Jul 12 '22
exercises/problems don't turn easy You become stronger and stronger over time
3
6
u/Lecterr Jul 12 '22
Gotta work on your indentation game yo.
3
u/Rheykar Jul 12 '22
True
2
u/TurintheDragonhelm Jul 13 '22
Get Prettier extension on VSCode and every time you save it will automatically format for you.
2
u/FunctN Jul 15 '22
It terms of formatting I would recommend learning to use a linter as well. For JS I use ESLint for all my projects and I have my own set of rules I follow so all my code across all projects have the same rules followed.
5
u/pekkalacd Jul 12 '22
I agree. Your happiness is unreasonable for this country club of a thread. You are kicked out for life for being too happy.
3
4
u/jimrooney Jul 12 '22
There is no such thing as "unreasonably happy" with building working code.
Every little piece of your exercise there is fundamental to actually using JavaScript.
This is the way.
2
2
u/taroicecreamsundae Jul 12 '22
one of my students was literally euphoric when she printed a sentence. it's pretty exciting and gratifying to see that you can create stuff like this. keep going!!
1
2
u/htraos Jul 12 '22
Pretty good! Now try to do the fizz-buzz!
1
u/Rheykar Jul 12 '22
I completed it not so long ago and it was fun. I had some problem with the "not a number" part 😅
2
2
Jul 12 '22
I've been learning and I'm just glad I could read your code and understand it without much thought. It's cool how everything starts to come together once you understand the basic building blocks.
1
2
u/hcabbos70 Jul 12 '22
Where are you learning? A site?
2
2
u/TP71899 Jul 12 '22
How does this work when i = 0? That doesn’t generate an error of some kind?
1
2
u/takishan Jul 12 '22
Just a minor nitpick.. set up autoformat on save in VS Code. I'm not sure if it's an extension you need or need to install some npm package or it's built into the VS Code settings. Just so everything's indendented properly. ie
function showNumbers(limit) {
let i = 0;
while (i <= limit) {
let number... bla bla bla
}
}proper formatting makes everything easier to read further down the line. congrats
2
3
3
u/Bulji Jul 12 '22
You could also use a for loop instead of a while loop, since you're using a counter and for loops have an integrated counter:
for(let i = 0; i <= limit; i++) {let numberType = i % 2 === 0 ? "EVEN" : "ODD";console.log(i, numberType);}
3
u/Rheykar Jul 12 '22
I wanted to use it first but I got confused because it was so compact. The while loop vertically explained itself for my beginner brain better. If this make sense 😅 But yes this looks cleaner
2
u/Bulji Jul 12 '22
Yeah for sure, your code is working just as well and uses the same looping logic anyways so in this case it's just syntax
2
u/net_nomad Jul 13 '22
You want to use a while loop when you don't know the iteration condition. Generally, if you can count it then you want to use a for loop.
So, iterating 100 times - for loop. Iterating until some variable is false - while loop.
It's important to start thinking about the right loop construct for the job at hand instead of just using what's comfortable.
16
u/el_bosteador Jul 12 '22
I love this stage of becoming a programmer. Later on it gets a little more complex but that feeling of satisfaction never goes away!
2
u/fhlarif Jul 12 '22
Of course! I would be too! I'm currently learning Typescript along with JavaScript and seeing your code makes me mentally want to try applying some type casting . Maybe you should try it too.
3
u/Rheykar Jul 12 '22
I tried to google "type casting" but right now it seems too much for me. Thanks for the feedback anyway :)
4
1
1
u/Tripping_on_100 Jul 13 '22
comment this please
1
2
u/shuckster Jul 13 '22
Does it really need commenting?
With the exception of the function name everything is self-explanatory.
3
u/_Invictuz Jul 13 '22
I don't know why but I thought the title said you became unreasonably unhappy. I even reread the title a couple times. It was only after I finished going through all the comments that I realized it said happy! Maybe I was projecting my unhappiness haha. Anyway, congratz!
1
u/LordTurin0011 Jul 12 '22
Congratulations..... You've a long journey ahead of you.... It'll seem tough at certain points, where you'll have no idea what you're doing.... But don't give up.... Just keep grinding.... Good luck....
1
2
u/HaroerHaktak Jul 12 '22
as a python user, this unnerves me because of the lack of indentations ;-;
2
u/acoustic_embargo Jul 12 '22
That's a cool little chunk of code.
For fun, I did a for loop-flavored refactor, and used truthiness/falsiness to avoid the === 0 bit:
function showNumbers(limit) {
for (let i = 0; i <= limit; i++) {
console.log(i, i % 2 ? 'ODD' : 'EVEN')
}
}
showNumbers(100)2
2
2
2
2
2
2
2
2
2
u/CadmiumC4 Jul 13 '22
You have an indentation mistake on the while loop but it is good overall. Congratulations
1
u/France_linux_css Jul 12 '22
What % means?
6
3
1
u/jayerp May 02 '23
As long as you are happy AND you understand why and what it’s doing why and what it’s doing, I am also happy.
1
u/France_linux_css Jul 12 '22
Let number =i % 2===0 what does this mean
6
u/ikeif Jul 12 '22
So,
console.log(0%2) // outputs 0, because 0/2 has 0 remainder console.log(1%2) // outputs 1, because 1/2 has 1 remainder console.log(2%2) // outputs 0, because 2/2 has 0 remainder console.log(3%2) // outputs 1, because 3/2 has 1 remainder console.log(4%2) // outputs 0, because 4/2 has 0 remainder console.log(5%2) // outputs 1, because 5/2 has 1 remainder
let numberType = i % 2 === 0could be written also as:let numberType = (i % 2) === 0(to show the math operation before the equality check).Or,
const remainder = i % 2 const numberType = remainder === 0Now, I switched it to
consthere because we are assigning it a value that will never change.
0
Jul 12 '22
I no shit get a brief buzz after completely something like FizzBuzz if I didn't have to study someones completed version.
I did rock paper scissors with Odin yesterday and I only got that buzz a few times from finally connecting stupid simple dots that I previously had not, but couldn't get the if statement chain formatted properly to work, so I felt the opposite of high, which is like an idiot.
0
0
0
50
u/FunctN Jul 12 '22
You should be happy! Looks awesome! I remember when I started learning I used to get so pumped as well!