Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to output determinated strings in an array calling a function in JavaScript

when the function is being called the keys (lisa, homer, etc) I know should be passed in slightly differently than it is currently, but I tried many things and I can’t figure out how to get it to return anything other than undefined

The expected console output should be:

// BAAAAAART! // Eat My Shorts! // d’oh!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// Returns undefined

Also, I’ve tried using console.log(jsObject[key]); within the for loop but still got undefined, so used [keys] instead which works but ignores the loop. Really struggling with this one!

let printValuesOf = (jsObject, keys) => {
  for (let i = 0; i <= keys.length; i++) {
    let key = keys[i];
    console.log(jsObject[key]);
  }
}

let simpsonsCatchphrases = {
  lisa: 'BAAAAAART!',
  bart: 'Eat My Shorts!',
  marge: 'Mmm~mmmmm',
  homer: 'doh!',
  maggie: '(Pacifier Suck)',
};

printValuesOf(simpsonsCatchphrases, 'lisa', 'bart', 'homer');

How would you fix this to pass the function? Thanks a lot for any help, and sorry for this I am just a a beginner

>Solution :

keys is not an array, keys is 'lisa', you wanted to use ... operator

let printValuesOf = (jsObject, ...keys) => {
  for (let i = 0; i < keys.length; i++) {
    let key = keys[i];
    console.log(jsObject[key]);
  }
}

let simpsonsCatchphrases = {
  lisa: 'BAAAAAART!',
  bart: 'Eat My Shorts!',
  marge: 'Mmm~mmmmm',
  homer: 'doh!',
  maggie: '(Pacifier Suck)',
};

printValuesOf(simpsonsCatchphrases, 'lisa', 'bart', 'homer');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading