5 Nested for Loops


#1

In this exercice in point 5 ,in the if statement , it is said that array 1 is equal to array than print the city in common BUT ! the problem is that two arrays are not equal ??? So why does it print the city in common ?
The exercioce was ok , but I still do not understand .


https://www.codecademy.com/courses/learn-javascript-loops/lessons/loops/exercises/for-loops-iii?action=lesson_resume&course_redirect=introduction-to-javascript
Thanks for your explanation !


#2

I’d fix the formatting of that before spending any amount of brain cycles on the issue. (Would write it properly formatted to begin with, nothing should ever look like that)

Anyway, if you were to either print it, or not, then the printing probably shouldn’t be in a loop, it’d be something you do after you’ve decided which to do.

You only need one loop for testing whether two arrays are equal, so what is the other one doing?

From the instructions:

The purpose of the program is to see what you and your friend have in common.

That doesn’t mean that the arrays need to be equal, and it does mean that there may be multiple prints


#3

Hi Ionathan !

… but isn’t it written in the instruction :

" Inside the second for loop’s block, write an if statement that checks if myPlaces[myPlacesIndex] is equal to friendPlaces[friendPlacesIndex " ?

thanks for your explanation !


#4

I didn’t say not to. The question is what you mean to do, and how you’d go about doing that.


#5

Ionatan ,
I have a second question for you . Could you answer it please ?
It’s about the point N°6 While Loops
It works perfectly but I m surprised to see both random cards appearing on the Terminal each time I click RUN and the sentence The program found a Spade ? Shouldn’t it be either one or the other ?
https://www.codecademy.com/courses/learn-javascript-loops/lessons/loops/exercises/while?action=lesson_resume&course_redirect=introduction-to-javascript


thanks a lot !


#6

Why would it be either one or the other? There’s no if-else in that code


#7

Also, rather than using 4, you’re better off using the length of the array. Better yet is to create a function for choosing a random element because that task is error-prone (best off done by itself, or better yet done by a library)
If you for example added a fifth card, or removed some, then you’d be unable to pick indexes at 4 or above, or you’d get index-out-of-bounds errors.

Similarly, your code in your first post shouldn’t log 'Paris', it should log whatever city is matching, that might be something other than Paris (You’re only able to log Paris directly because you already know the end-result – which is really just saying that you don’t need to write a program for it


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.