Difficulty With Object Coding

var cars = {
    colors : ["black","blue","red","yellow"],
    style : "sleek",
    model : "Cadillac",
    wheels : {
        tires : "Goodyear",
        rims : "steel", }
fill(18, 120, 105);
text("Cars, Cars and More Cars",10,25);
text("This year's models are " + cars.style + " and come in " cars.colors[0] + " and "
 + cars.colors[1] + ".",10,15);

// the code was supposed to print out - Cars, Cars and More Cars (which it did) and
This year’s models are sleek and come in black and blue (which it didn’t) - any idea of why this sentence didn’t print ?

You’re missing a plus sign (+) after " and come in "

John - you may find using the template literal approach outlined here to be easier and less error prone: https://www.kirupa.com/javascript/combining_strings_variables.htm


Ah … … I thought I was being so careful when I wrote the code - my proofreading skills obviously need sharpening - thanks for catching and pointing out the omission.

terrific - I will study the contents of the link provided - thanks

It can be hard to notice these things, especially when you’re still trying to familiarize yourself with the syntax.

Many editors nowadays will have, or can have via plugins, linters that will check your code for mistakes like this as you type. A popular one for JavaScript is ESLint. Putting your code through that linter shows the error as a parsing error (if you look in the code, there’s a little red squiggly where the error is).

Really ? I don’t see a little red squiggly in line 13 where the error is - on the other hand it does point out the error as being in line 13 and the 63 in 13:63 I assume stands for the position of the error in the line so that is pretty darn good

Ah interesting. Im using Chrome on Mac and the I see the squiggly

It also shows in VS Code.

I’m using Safari so that could be the difference but at any rate Linter does identify the issue quite precisely so it’s very useful in that regard.

1 Like