hello everyone;
I’ve been study JS for a couple of months now, I find myself struggling about to think logical for JS.
Let me try to explain myself better with a example . When I got the exercises to do either her on FCC or on the W3shool one, i fell I am fine while reading the content then I can’t solve the exercise and once I saw the answer it looks so obvious that I fell Ashamed I didn’t think about that without help.
There anyone here who struggling to find a way to thinking JS? If so what helps? Have you some tips or anything?
Thank you!
before thinking in JS, try just to write by hand what’s the input and the corrisponding output, and what are all steps needed to go by one to the other.
Once you have a working algorithm, and only then, you convert it to JavaScript
Hey @PollyLopes!
Welcome to the forum!
Not only are you trying to learn JavaScript but you are also learning programming and problem solving. That takes times to learn. I would practice what @im59138 said about algorithms especially when you get to the algorithm sections and final projects. It will really help out a lot. I would also suggest having documentation open sometimes when you are trying to solve the problem. I use MDN docs alot.
Also, javascript can be weird at times. . So it is total normal to struggle with it.
I won’t bother repeating the good advice already given. But I will say that what you are experiencing is very normal. This is hard stuff - that’s why it pays well. It takes time to learn this stuff. I’ve been studying JS for 4.5 years now and been a professional dev a little more than 2 years and I’m still learning things - there are still things that are a little fuzzy.
@PollyLopes Welcome to the forums! Some very great people have been helping you out and I am just going to try and add my two cents from personal experience. I was working through the JavaScript Algorithms and Data Structures certification a couple months ago. I had this same problem. I would take things to fast and get angry and always look at the hints and answers. All that did for me was shortcut my learning. I did away with this habit quickly. I found a lot of support on the FCC Discord community and was able to work through the algorithms with a person there. He really helped me understand and break things down. I would say just keep going. Break things down. Take your time. It will pay off.
Hello @PollyLopes,
I find myself struggling about to think logical for JS.
I think this is probably related more with teaching programming using examples[0] than with you.
The idea of teaching by examples is :
if the student try enough times, he will end up discovering/stumbling with “general solutions for similar problems” (most of the time this will not happend).
But, there is another way: teaching recipes that enables to the student to solve problems[1].
Spoiler Alert!:
“Intermediate Algorithm Scripting: DNA Pairing”
You can find this solution in internet:
Example Driven ( teaching):
Hint: 2
I would recommend using a switch, as it makes things a lot smoother.
Code Explanation:
The program is very simple, the best solution that I have come up with is to use a switch to catch all the possible four elements. Using if statements would take too much code. You could also use Regular Expressions.
You can see, this is very specific.
This can be used in other problems? yes.
But how do you generalize the hint and the code explanation?
Recipe Driven (teaching):
You can represent information as data using:
a.- enumerations
- collection with a finite number of element
- lists every single piece of data that belongs to it
b.- intervals
- collections of elements that satisfy a specific property
- collection with infinitely many elements
- specifies a range of data
c.- itemizations
- specifies ranges in one clause of its definition
- and specific pieces of data in another clause
In the “Intermediate Algorithm Scripting: DNA Pairing” exercise we can use:
a.- enumerations
- collection with a finite number of element
- lists every single piece of data that belongs to it
Defining enumerations means distinguishing among different kinds of elements.
- To distinguish in code requires:
- conditional functions:
functions that choose different ways of computing results depending on the value of some argument.
- conditional functions:
The main idea of an enumeration is:
- it defines a collection of data as a finite number of pieces of data
Each item explicitly:
-
Spells out which piece of data belongs to the class of data that we are defining.
A data representation in which every possibility is listed -
When a function’s input is a class of data whose description spells out its elements on a case-by-case basis:
- The function should distinguish just those cases
- and compute the result on a per-case basis
Example (HTDP uses scheme
):
(define (traffic-light-next s)
(cond
[(string=? "red" s) "green"]
[(string=? "green" s) "yellow"]
[(string=? "yellow" s) "red"]))
in JS:
function trafficLights(s) {
switch(s) {
case 'red':
console.log('green');
break;
case 'green':
console.log('yellow');
break;
case 'yellow':
console.log('red');
break;
}
}
A template for enumerations:
function name(arg) {
switch(arg) {
case '':
// computation
break;
case '':
// computation
break;
case '':
// computation
break;
default:
}
}
What you need to do to solve the exercise:
- Use the template
- Distinguish the cases (switch)
- compute the result on a per-case basis
This is different? yes:
- You have a template/recipe that you can follow (no writer block)
- If you can’t get the solution, you need to start again (maybe is a different kind of problem, you need to modify the recipe , use a different data representation,etc.)
- You can use the same recipe for similar problems
My point is: this is not about “JS logic”, is about techniques/skills that you can learn an practice.
notes:
- [0] I wrote a blog post about it:
https://diegoperezm.github.io/blog/#the-problem-with-example-driven-teaching-and-gamification
-
[1] How to Design Programs, Second Edition:
-
35C3 - How to teach programming to your loved ones