r/JavaScriptTips • • Jan 21 '23

debugging this JavaScript code Good day everyone. I am currently learning JavaScript, I started 2 months ago. I humbly request your assistance in debugging the code below. I tried watching YouTube but I couldn't get any assistance. So the code is supposed to out BAART! Eat my shorts ! O'oh! Bu

Post image
3 Upvotes

3 comments sorted by

3

u/slam_the_damn_door Jan 21 '23

Line 21 - 'lisa', 'bart', 'homer' should be an array not individual strings.

['lisa', 'bart', 'homer']

Line 14 - console log you need to key the variable jsObject with the element at the ith position of the passed keys array.

console.log(jsObject[keys[i]])

Idk what your trying to do on line 18. If you remove it that should run.

1

u/xuy87 Jan 21 '23

There are different approaches, but following this approach, also replace <= with < in line 13

1

u/Banzaye Jan 27 '23 edited Jan 27 '23

You don't need to run a for loop if you are passing only certain phrases:

const phrases = { a: 'Hello', b: 'World', c: '!' }

function printPhrases(one, two, three) { console.log(one + ' ' + two + ' ' + three) }

printPhrases(phrases.a, phrases.b, phrases.c)

If you don't want to be limited in your arguments you can use:

const phrases = { a: 'Hello', b: 'World', c: '!' }

const printPhrases = (...args) => { args.forEach(arg => console.log(arg.toString())) }

printPhrases(phrases.a, phrases.b, phrases.c)