Javascript Challenges

Check properties

We have the following code...

var key,
    obj = {
        name: 'john',
        surname: 'doe'
    };


for ( key in obj ) {
    if ( obj.hasOwnProperty( key ) ) {
       console.log( key + ' exists in obj' );
       console.log( key + ': ' + obj[key] );
       continue;
    }
    console.log( key + " doesn't exist in obj" );
}

... and the result of executing it is...

name exists in obj
name: john
surname exists in obj
surname: doe

... but we want to get the following result, can you help us?

name doesn't exist in obj
surname exists in obj
surname: doe

Exercise

Correct!
False!

Write the code to get the expected result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var key,
    obj = {
        name: 'john',
        surname: 'doe'
    };
for ( key in obj ) {
    if ( obj.hasOwnProperty( key ) ) {
       console.log( key + ' exists in obj' );
       console.log( key + ': ' + obj[key] );
       continue;
    }
    console.log( key + " doesn't exist in obj" );
}
X