Tweaking Comparing Arrays Exercise


#1

Just 'doing some laps' on earlier exercises, trying to gain more 'coding memory' and allow 'memorized syntax' to start to become 'realized syntax', I was just trying to give a minor tweak to the comparing arrays exercise by just adding an 'else' statement so if there is no shared values in the two arrays. My problem is, despite my !== assignment, it still console logs whether theres a shared value or not. My guess would have been refinement in my spec qualifications as if there is only one shared value, the computer may still be seeing differences that allow for 'else' to cue/execute.

What left me entirely lost was when I made the arrays EXACT and it still executes. Hmph!!! As well as Hmmmmm!!! Appreciate the help!

FWIW, I feel I've exhausted all the obvious bracket/assignment arrangements and yet still...again, thanks!


var myArray=['julie', 'her fam', 'cindy'];

var herArray=['julie', 'her fam', 'cindy'];

for(var me=0;me<myArray.length;me++){console.log(myArray[me]);{

for(var her=0;her<herArray.length;her++){console.log(herArray[her]);{
    
if(myArray[me] !== herArray[her]){console.log('nada');}
else {console.log('we have a match, it is ' + myArray[me]);}}}}};


#2

I'm confused on what you're having trouble with...

So if there is no shared values in the two arrays what? What do you want to happen if there are not shared values?

of course it does because inside of your if statement, you tell it to print 'nada' if there's no shared value.

This is more of a sidetone, but I just want to say it's great you are trying to go back and relearn some of the material to become more familiar with it. However, avoiding line breaks like you do with your loops and if/else statements may result in less lines, but it is not the best practice. It is fine to do that all you want, because it can be clever, but that's not what you would want to use to optimize your code. :slight_smile: Just my opinion. take it or leave it.


#3

Yeah, fix that formatting before doing anything else. While @kyleaw is entirely right about that you're free to get creative about such things, it's pretty much only done to intentinonally make the code difficult to understand or to save precious network traffic but then that's done by running the code through a program that rewrites the code into something equivalent but smaller. When sharing code / learning it's not what you want to be doing, and as said, probably not otherwise either.

Have a look at just about any js style guide or examples in documentation, this one looks reasonable (no advanced code is used and it's fairly short): http://www.w3schools.com/js/js_conventions.asp


#4

Hey,

I appreciate the response! Sorry if my question is unclear or if its just obvious the biggest issue is i just need more practice. I understand I was not exactly enhancing utility to the function but I'm still a bit confused about my superfluous "if" statement. To my rudimentary understanding, I'm telling the computer that if my two arrays have no shared values then for it to print 'nada', but to me, ALL the values are the same ('Cindy', 'her fam', 'Julie') so it should skip over that it go onto the next block of code ('we have a match, it is ' + var[array]). Otherwise I'm again kinda lost as to how the computer is able to identify the shared value when I don't try to get cute and show in another conditional statement...again sorry if none of this makes sense. I'm steadfast on my tinkering which is always rewarding in revealing small but big a-ha moments. I'm sure one will come for this in time! :slight_smile:

  Thanks

#6

The first thing to fixing the code is to look at it, and for that the formatting needs to be sane.

Reformatted code:

var myArray = ['julie', 'her fam', 'cindy'];
var herArray = ['julie', 'her fam', 'cindy'];

for (var me = 0; me < myArray.length; me++) {
    console.log(myArray[me]);
    for (var her = 0; her < herArray.length; her++) {
        console.log(herArray[her]);
        if (myArray[me] !== herArray[her]) {
            console.log('nada');
        }
        else {
            console.log('we have a match, it is ' + myArray[me]);
        }
    }
}

You're comparing each element in one array to each of the other. For two arrays of size 3, that's 3 * 3 iterations, 9 in total.

Each time that you do that comparison, you either print that there is a match, or that there isn't.

3 combinations match, 6 don't. You'll have three prints saying match, and three saying the other thing which is nada.


#7

Thanks for that breakdown..think it's gonna help me actualize the process much more. Very much appreciate the 'sane' reference. lol...reminds me of seeing first day rock climbers at the gym and their approach/technique...no substitute for practice. But first n most important step is entering to begin with...that's kinda where I'm at now. Thanks again!

----- Reply message -----


#8

I really appreciate that break down response. Crystallized exactly where my misinterpretation was. I remember the lesson explaining the order in which the computer would run the arrays but the 'consequence' of that with my tacked on tweak...I wasn't connecting the dots with until your reply. So far it's fun to successfully execute things but the real rewards have come in the a-ha moments. Your explanation provided one! Mucho thanks!

Justin

----- Reply message -----


#9

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