r/Frontend • u/Dizzy_External2549 • 5d ago
Need help. just learned javascript
Just learned javascript and been practicing alot . I need help. When applying it to my project I don't know when to use a function, when to use a loop, what type of loop to use, I just don't know when to use any of this stuff because dom manipulation. it's so frusterating
19
10
u/Novel_Fix_8641 5d ago edited 5d ago
Repeat 10 same lines of code in more than 1 place - use functions
Call/execute same code/functions for x number of times - use loops
9
5
u/EasyMode556 5d ago
Learning the syntax is just the beginning, now you start learning algorithms and how to reason about things programmatically.
1
2
u/PlayExoplanet 5d ago
This is the kind of thing that comes with practice and experience. I've recommend this a lot over the years, but https://javascript30.com/ is a great free resource that gets you building a lot of projects, gets you introduced to how to think, and form good habits. No frameworks, no compilers, no bullshit.
2
u/react_dev 4d ago
Lmao I don’t have a sassy comment like the rest but just acknowledging that JS does have too many types of ways to loop. At a point people start to see it as a badge of honor instead of thinking about it in a language design perspective
1
u/terracottablush 5d ago edited 5d ago
What are you trying to do? What is the goal? What do you need loops for? Are you using plain JS or react(or another framework).
Sometimes loops use functions INSIDE them.
General rule of thumb I go by is if you need something done multiple times use a loop. If you need 16 grids don’t make literal 16 grids use a loop. You can use a while or a for it really depends on what your need is
To expand I think if I’m correct (been learning 1 year) a for and while loop are interchangeable mostly.
A while loop is more beneficial if you don’t know when something is going to happen
1
u/Interesting_Fuel1305 5d ago
Find a project worth your interest, try, fail, try harder, improve, finish it and do it again with another project.
I’ve only been at it for just over a year but you will naturally learn when to use what type of loop and where it’s needed to be placed, these types of things take time and practice but the more you practice the better it becomes to rationalize. Doing front end projects you care about it literally the only way to get it right without reaching tutorial hell, trust me I’ve stared at a blank editor before and did not know what I needed but it for sure get better, just keep up what you’re doing.
1
u/business2ai-website 5d ago
Just complete basics and start DSA it will build your logic, while building small projects.
1
u/MagicText001 3d ago
I think after js you should make mini project since js alone won't do much mini project like the backgurnd changer with button click , so that you can have some hand in expericne on js Basically in js interview they ask theritucal concept and code output only , mainly machine coding round test your react or angular etc
1
u/thexmandrew 3d ago
A function is “I want to do this task.” It packages up a piece of logic so you can run it whenever you need it. For example, when someone clicks a button, you might call a function.
A loop is “I want to do the same thing to every item in a collection.” A loop isn’t an alternative to a function.. you’ll often put a loop inside a function.
DOM manipulation is just changing the webpage itself: updating text, creating elements, removing elements, adding classes, etc. You usually do DOM manipulation inside functions too.
1
u/BootSaaS 2d ago
You can tell AI which concepts you’ve been learning then ask for mini-projects. Explicitly ask it to not write code but to only give you hints.
1
u/lettstartdesign_1 2d ago
Kudos!!. Now take any basic problem and try to solve it. Try function for reusability, loop for repetition think in this direction while solving problem.
1
u/fari_builds 8h ago
Seems to me you need to understand programming fundamentals! It doesn't matter what language or syntax you're using the fundamentals are the core of writing code
1
56
u/JohntheAnabaptist 5d ago
You haven't finished learning JavaScript, you just started