Advertisement
makispaiktis

Objects - For loops and correct syntax

Oct 19th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let spaceship = {
  2.     crew: {
  3.     captain: {
  4.         name: 'Lily',
  5.         degree: 'Computer Engineering',
  6.         cheerTeam() { console.log('You got this!') }
  7.         },
  8.     'chief officer': {
  9.         name: 'Dan',
  10.         degree: 'Aerospace Engineering',
  11.         agree() { console.log('I agree, captain!') }
  12.         },
  13.     medic: {
  14.         name: 'Clementine',
  15.         degree: 'Physics',
  16.         announce() { console.log(`Jets on!`) } },
  17.     translator: {
  18.         name: 'Shauna',
  19.         degree: 'Conservation Science',
  20.         powerFuel() { console.log('The tank is full!') }
  21.         }
  22.     }
  23. };
  24.  
  25. // CORRECT
  26. for(let crewMember in spaceship.crew){
  27.     console.log(`${crewMember}: ${spaceship.crew[crewMember].name}`);
  28. }
  29.  
  30. //WRONG
  31. for(let crewMember in spaceship.crew){
  32.     console.log(`${crewMember}: ${crewMember.name}`);
  33. }
  34.  
  35. // CORRECT
  36. for(let crewMember in spaceship.crew){
  37.     console.log(`${spaceship.crew[crewMember].name}: ${spaceship.crew[crewMember].degree}`);
  38. }
  39.  
  40. // WRONG
  41. for(let crewMember in spaceship.crew){
  42.     console.log(`${crewMember.name}: ${crewMember.degree}`);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement