Given the following Javascript objects, log individual values using dot and bracket notation.
Last active
October 28, 2022 05:48
-
-
Save acidtone/f4ff695f2db428344549e1ae045e0439 to your computer and use it in GitHub Desktop.
JS Activity: Object-oriented Treasure Hunt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const guild = [ | |
{ | |
name: 'vestia', | |
class: 'bard', | |
race: 'elf', | |
image: 'vestia.png', | |
health: 57, | |
status: { | |
poisoned: true, | |
fire: false, | |
frozen: false, | |
stunned: true, | |
enchanted: false | |
} | |
}, | |
{ | |
name: 'ocarina', | |
class: 'cryptobro', | |
race: 'human', | |
image: 'ocarina.png', | |
health: 50, | |
status: { | |
poisoned: false, | |
fire: false, | |
frozen: false, | |
stunned: false, | |
enchanted: false | |
} | |
}, | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************/ | |
/* Example 1 */ | |
/*************/ | |
const student = { | |
name: 'John', | |
age: 20, | |
marks: { | |
science: 70, | |
math: 75 | |
} | |
} | |
// Student's age | |
console.log(student.age); // 20 | |
// The student's name | |
console.log('Student name here.'); // 'John' | |
// The student's mark in math | |
console.log("Student's math mark here."); // 75 | |
/*************/ | |
/* Example 2 */ | |
/*************/ | |
const business = { | |
company: 'Ship and Anchor', | |
address: { | |
building: 534, | |
street: '17 Ave SW', | |
city: 'Calgary', | |
postal: 'T2S 0B1', | |
province: 'AB', | |
country: 'CA' | |
} | |
}; | |
// Company Name | |
console.log(business.company); // 'Ship and Anchor' | |
// City | |
console.log('City here.'); // 'Calgary' | |
// Building Number | |
console.log('Building number here.'); // '534' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************/ | |
/* Example 1 */ | |
/*************/ | |
const character = { | |
name: 'vestia', | |
class: 'bard', | |
health: 57, | |
stats: { | |
strength: 10, | |
agility: 14, | |
constitution: 11, | |
intelligence: 16, | |
charisma: 8 | |
}, | |
backpack: [ | |
'rope', | |
'rations', | |
'bag of holding' | |
], | |
equippedItems: { | |
head: 'helmet of smarts', | |
leftHand: 'dagger', | |
rightHand: null, | |
armor: 'leather', | |
feet: 'steel-toed boots' | |
}, | |
statusEffects: { | |
poisoned: true, | |
fire: false, | |
frozen: false, | |
stunned: true, | |
enchanted: false | |
}, | |
isEnchanted: function() { | |
return this.statusEffects.enchanted; | |
} | |
} | |
// Character's Intelligence | |
console.log(character.stats.intelligence); // 16 | |
// Second item in the character's backpack | |
console.log(character.backpack[1]); // 'rations' | |
// Character's name, capitalized | |
console.log('Character name here.') // Vestia | |
// The number of items in the character's backpack | |
console.log('Number of items') // 3 | |
// Is the character's right hand empty? true or false | |
console.log('Is right hand empty?') // true | |
// Is the character enchanted? true or false | |
console.log('Is enchanted?') // false | |
// Remove rope from the character's backpack | |
console.log('Return the character but with no rope in backpack'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************/ | |
/* Example 1 */ | |
/*************/ | |
const student = { | |
name: 'John', | |
age: 20, | |
marks: { | |
science: 70, | |
math: 75 | |
} | |
} | |
// Student's age | |
console.log(student.age); // 20 | |
// The student's name | |
console.log('Student name here.'); // 'John' | |
// The student's mark in math | |
console.log("Student's math mark here."); // 75 | |
/*************/ | |
/* Example 2 */ | |
/*************/ | |
const business = { | |
company: 'Ship and Anchor', | |
address: { | |
building: 534, | |
street: '17 Ave SW', | |
city: 'Calgary', | |
postal: 'T2S 0B1', | |
province: 'AB', | |
country: 'CA' | |
} | |
}; | |
// Company Name | |
console.log(business.company); // 'Ship and Anchor' | |
// City | |
console.log('City here.'); // 'Calgary' | |
// Building Number | |
console.log('Building number here.'); // '534' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment