r/learnjavascript • • Jul 12 '22

I just completed a pretty basic exercise and became unreasonably happy

Post image
440 Upvotes

79 comments sorted by

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!

8

u/Rheykar Jul 12 '22

Yeah, it looks like a good source for that :D

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

u/andrejmlotko Jul 12 '22

Wow, looks really good! Congratulations!

5

u/Rheykar Jul 12 '22

Thanks ^^

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

u/Rheykar Jul 12 '22

I'm quite a beginner but I'll try when I get there

7

u/holdyrbreath Jul 12 '22

exercises/problems don't turn easy You become stronger and stronger over time

3

u/Rheykar Jul 12 '22

Thanks for advice!

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

u/Rheykar Jul 12 '22

Hehe :D

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

u/Rheykar Jul 12 '22

I hope to get there

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

u/Rheykar Jul 12 '22

It is kind of a special feeling however simple it is. Helps a lot

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

u/rm-rf-npr Jul 12 '22

Aaah the good old days when life was simpler. Nice and congrats!

1

u/Rheykar Jul 12 '22

Thanks 😄

2

u/[deleted] 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

u/Rheykar Jul 12 '22

Right, as if you had received a magical eyesight :D

2

u/hcabbos70 Jul 12 '22

Where are you learning? A site?

2

u/Rheykar Jul 12 '22

I'm learning on YouTube. There are many tutorials fortunately

2

u/hcabbos70 Jul 12 '22

Excellent. Thank you.

2

u/TP71899 Jul 12 '22

How does this work when i = 0? That doesn’t generate an error of some kind?

1

u/Rheykar Jul 12 '22

I think in this case 0 is simply a number value and nothing else.

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

u/Rheykar Jul 12 '22

Thanks for the advice!

3

u/shuckster Jul 12 '22

With great power comes great responsibility.

2

u/Rheykar Jul 12 '22

A wise advice 😮

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 :)

1

u/Soft-Sandwich-2499 Jul 12 '22

Congrats, keep learning 💪

1

u/Rheykar Jul 12 '22

Thanks! I try to motivate myself

1

u/Tripping_on_100 Jul 13 '22

comment this please

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

u/Rheykar Jul 12 '22

Thank you!

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

u/zulmetefza Jul 12 '22

Now do it for 101 if you can!

2

u/empolem Jul 12 '22

just wait till you start doing codewars

2

u/felixWalker36 Jul 12 '22

Why didn't you turn NSFW on, that function is so sexy

2

u/mirkinoid Jul 12 '22

Actually you don’t need the index.html to run that

2

u/Oasishurler Jul 12 '22

Yeah, this is a good one. Lots of output

Mmmmmmh

2

u/codemonkeyhopeful Jul 12 '22

We all start somewhere, keep it up you got this.

2

u/dgd5014 Jul 13 '22

Nice job! That’s a good implementation for that problem. Keep it up!

2

u/[deleted] Jul 13 '22

Start of something great for you!

2

u/[deleted] Jul 13 '22

That's where we always start 👌👌

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

u/big_red__man Jul 12 '22

Modulus. It's the remainder after division. So 5%4=1

3

u/Mr_Lickety_Split Jul 12 '22

But 21 % 4 is also 1

1

u/end233 Jul 12 '22

That’s indeed 21%4 is 21-20 so the result is 1

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

What is let?

What is %?

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 === 0 could 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 === 0

Now, I switched it to const here because we are assigning it a value that will never change.

ES6 In Depth: let and const

0

u/[deleted] 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

u/[deleted] Jul 12 '22

[deleted]

2

u/Rheykar Jul 12 '22

Yes, the story of my life :D

1

u/_Invictuz Jul 13 '22

You got blessed by the man himself!

0

u/[deleted] Jul 12 '22

[deleted]

2

u/Rheykar Jul 12 '22

Some really genuine words here. Thanks ^^

0

u/[deleted] Jul 12 '22

[deleted]

5

u/Rheykar Jul 12 '22

I'ts called visual studio code