Can anyone explain in code challenges: intermediate JavaScript exercise 10/14? The first answer kinda makes sense. But, the second answer (commented out) is a real cluster to me.
Please post a link to the exercise.
Using nested loops we can iterate both lists and pick out the items that repeat in both lists. Letâs set up an empty list to hold our results.
a = [1, 4, 7, 9, 11, 15, 19, 20]
b = [2, 4, 6, 11, 12, 17, 19, 21]
z = []
for x in a:
for y in b:
if x == y:
z.append(x)
print (z)
# [ 4, 11, 19 ]
Making any sense of this? There are other approaches but I like to start with the most naĂŻve form and go from there, assuming the code works.
Thank you very much for the response. But, is this some sort of advanced js? I donât recognize a lot of the syntax. On a broader thought, is there anywhere I can learn js like this? Iâm just a beginner.
Thanks again,
Diabe
It could be considered as pseudo-code. JS has the exact same syntax, just slightly different. (Weâre looking at Python.)
The first three lines can be either language, JS or Python.
The for loops can be ported to JS:
for (x of a) {
for (y of b) {
if (x === y) {
z.push(x)
}
}
}
console.log(z)
Aside
If, as you say you are only beginning then leave the challenges to a later time, after youâve got some learning under your belt. Donât expect to learn from code that is over your head. Work your way up the ladder and eventually it will no longer be that way, and you will be able to work through the challenge rather than âfigure it outâ.
Also note:
Notice that in
and of
are both terms that semantically imply membership? Put an N. B. on this similarity.
JavaScript does have an in
operator but it applies to objects, not arrays. Pythonâs in
operator applies to all traversable structures, but I digress.
Bottom line, start at the beginning and do not skip any steps. It is all foundational material that you will need to explore and practice until it hurts before you gain any competence or confidence. Donât let confusion belay your confidence. Seek it and your confidence will increase as you work things through. Itâs that process that gives you competence. I cannot think of any other process that short-circuits this one. One person in a million is a true fast learner. The rest of us have to go pretty much by the book in terms of this process.
I hear what youâre saying. Unfortunately, these challenges are the next step in the Codecademy plan for frontend engineer and cannot be skipped. But, they throw things at you that have not been covered in the exercises.
If you have not taken the Learn HTML, Learn CSS and Learn JavaScript beginner courses then I suggest pause this track and swing around to those courses. It will be about a two to three week segue but worth the time.
I have. Iâm on intermediate js now. Itâs just that the challenge questions often use code that has never been explained. Itâs frustrating!
There is no harm in retracing oneâs steps, even as far back as the beginning. I donât see any difficulty in the code example above once we learn how to use loops and conditionals. Like I said earlier, challenges are not lessons, but new terrain to explore and apply our new knowledge. We donât expect to learn concepts from scratch but to learn new ways to envision the concepts weâve been adding to our toolkit.
I understand the concept of âchallenge questionsâ. Itâs to take what youâve already learned and apply it to a new paradigm. Unfortunately, the questions sometimes (not always) cover concepts that havenât been previously covered. Not good.
This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.