These are chat archives for FreeCodeCamp/HelpJavaScript

30th
Jun 2018
Christopher McCormack
@cmccormack
Jun 30 2018 00:00
that isn't the greatest solution but should work
for one thing, contacts[i].hasOwnProperty( prop ) == false) is redundant, hasOwnProperty returns a boolean value, you can just omit the == false and it would still work
Amit Patel
@AmitP88
Jun 30 2018 00:01
I've seen other people use a nested for loop as well
Christopher McCormack
@cmccormack
Jun 30 2018 00:01
you can also add an inner if statement to check for the property if the name does match so you don't have to check again in the else if
Amit Patel
@AmitP88
Jun 30 2018 00:02
I tried that just now (removing the == false from the hasownproperty) and it failed the last test
ah I see
Christopher McCormack
@cmccormack
Jun 30 2018 00:02
I'd have to see your code but it should absolutely work
Amit Patel
@AmitP88
Jun 30 2018 00:03
function lookUpProfile(name, prop) {    
    for(var i = 0; i < contacts.length; i++) {
        if( (contacts[i]["firstName"] == name) && (contacts[i].hasOwnProperty( prop )) ) {
            return contacts[i][prop];

        }else if( (contacts[i]["firstName"] == name) && (contacts[i].hasOwnProperty( prop ) )) {
            return "No such property";
        }
    }

    return "No such contact";
};
this didnt work
profile-false.png
Christopher McCormack
@cmccormack
Jun 30 2018 00:06
oh yes sorry you have to negate it
(!contacts[i].hasOwnProperty( prop ) ) notice the !
Amit Patel
@AmitP88
Jun 30 2018 00:07
ah I see. I was wondering about that, since wouldn't (contacts[i].hasOwnProperty(prop)) automatically evaluate to true?
or at least imply it
Christopher McCormack
@cmccormack
Jun 30 2018 00:08
well no not if you hit the else if
if you have the same conditional in both, the second will never get hit
Amit Patel
@AmitP88
Jun 30 2018 00:09
I see. It sort of makes sense, but I need to spend more time looking at it.
I usually don't have that much problems with arrays and objects, but this challenge was tough for me
that and Record Collection, which I'm still working on
Christopher McCormack
@cmccormack
Jun 30 2018 00:10

@AmitP88 a much cleaner and easier way to write it would be

function lookUpProfile(name, prop) {    
    for(var i = 0; i < contacts.length; i++) {
      if(contacts[i]["firstName"] == name) {
        if (contacts[i].hasOwnProperty( prop )) {
          return contacts[i][prop]; 
        }
        return "No such property"; 
      }
    }

    return "No such contact";
};

Note I didn't test this but I remember doing it something like this

Try to work through them slowly, sometimes sitting back and just walking through the statements, maybe writing down what you think each valuable should contain, is the best way to determine where you have issues in your code
Amit Patel
@AmitP88
Jun 30 2018 00:13
yeah, that look similar to what I've seen in the forums
definitely. Today I'm going over previous challenges that I've had to look up solutions to or that I just didn't get. I'm definitely going to take my time on them
Christopher McCormack
@cmccormack
Jun 30 2018 00:14
Great idea
Amit Patel
@AmitP88
Jun 30 2018 00:17
I think I'm gonna take a break, then come back to study the solutions. Thanks for your help though, I was super confused on this problem on why my solution didn't work, but it's clear now
Christopher McCormack
@cmccormack
Jun 30 2018 00:18
That's good, good luck!
Amit Patel
@AmitP88
Jun 30 2018 00:18
thank you :)
abraham anak agung
@padunk
Jun 30 2018 00:50
:point_up: June 29, 2018 5:43 PM @alpox Thanks, took me sometime to really understand how it works.
Mois├ęs Man
@moigithub
Jun 30 2018 15:25
Randy
@RandyGoldsmith
Jun 30 2018 18:30

Create a function named numberTypes() that accepts an array of numbers as its only parameter. If every number in the array is even, it should return 'even', if every number in the array is odd, it should return 'odd', if the array has both even and odd numbers it should return 'both'.

Where do I start? I know to loop through each number using map and I thought about using if else statements to see if the number is even and odd, but I don't know how to include a case and "save" if i found both an even and odd number and then return both.

Blauelf
@Blauelf
Jun 30 2018 20:35

@RandyGoldsmith You could have a boolean variable for odd numbers, initially true, and set it to false on encountering an even number. And a second boolean for the other way around. Then, after your loop, you could check the value of both booleans.

map is a weird version, but would work as well, e.g. you could map with n=>(n%2+2)%2 (for all non-negative numbers n=>n%2 would be enough) and sum up the results. If the result is 0, all are even. If result is same as array length, all are odd. Could do the same with a for loop.