Code challenge js question

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.

1 Like

https://www.codecademy.com/journeys/front-end-engineer/paths/fecj-22-building-interactive-websites/tracks/fecj-22-javascript-syntax-part-ii/modules/wdcp-22-practice-javascript-syntax-arrays-loops-objects-iterators-3f82e7b8-a851-45e7-b4ca-57901365edcc/lessons/intermediate-javascript-coding-challenge/exercises/just-cool-stuff

1 Like

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

1 Like

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.

1 Like

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.

1 Like

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.

1 Like

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.