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.

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" ); }