Code Is Not Printing as it Should

var cars = {

colors : [“black”, “red”],

models : [“pontiac”, “cadillac”],

year : “1970”

};

fill(55, 110, 10);

textSize(20);

for(cars.year = 1970; cars.year <= 1980 ; cars.year++) {

var y = 25;

text("The " + cars.colors[0] + cars.models[0] + " is from " + cars.year + “.”, 10, y);

y = y + 25;

}

This is supposed to print out 11 lines

The black pontiac is from 1970.

.

.

.

.

The black pontiac is from 1980.

I ran the code through Linter which tells me that the problem is in line 11 with an unexpected character at the 75 position

text("The " + cars.colors[0] + cars.models[0] + " is from " + cars.year + “.”, 10, y);

but just looking at it (I can’t pinpoint the 75 position with accuracy) I don’t see anything amiss with that line. In any case, nothing is printing out - why ?

I’m guessing its probably a quote character issue. Character 75 is the start of the "." string. The quotes in your post are typographic or “smart” quotes. These aren’t supported by JavaScript code, only neutral or “dumb” quotes are. Conversions of quotes can happen inadvertently as text is copied and pasted between document editors or if it for some other reason goes through a formatting step. When copying code from one place to another, you’ll want to make sure the quotes are correct. Additionally, when you write it, you’ll want to make sure you write it in a code editor that doesn’t make the conversions of quotes for you automatically.

Here is your code in the eslint linter with the quotes fixed and it passes just fine.

P.S. if you want your code here on this forums to be code formatted, put them between code blocks defined by three back ticks (```) with an optional language name after the first 3:

```javascript
...put your code here...
```

That will format the text for javascript code. You can also skip the “javascript” part which will cause it to automatically determine the kind of formatting which is also usually good enough.

```
...javascript code, probably...
```

Wow ! This is getting more mysterious all the time ! First of all -

do I even have the option of choosing between dumb and smart quotes when I am typing on my iMac? All I have on my keyboard are double quotes and a single quote on the same key and you get the double by using the shift key. I am guessing that the double quotes on the iMac are dumb as this problem has not surfaced before and as you said, it’s likely a result of the copy and paste process.

even with all my prior correspondence on this forum, the dumb quote issue was never a problem, but it’s good to know of the

‘’’ javascript

code

‘’’

approach.

this code - that you referred me to which was put through Linter, is fine insofar as printing a line but that line seems to have all 11 of the year numbers superimposed so I’m just getting one line printing out rather than 11 distinct lines. I would have thought that the
var y = 25; and
y = y + 25;
would have taken care of the spacing along the y axis but apparently not. Any insights on this ?
Your advice is much appreciated.

The keys on your keyboard should be dumb quotes (single and double quote key). The conversion usually only happens automatically in programs like Microsoft Word or something like that, though you can also turn that feature off there. I’m not sure why you’re running into it now or even…

Somehow it looks like you’ve managed to use smart quotes for that too :wink: the three quote-like characters there are backticks (`), which is the SHIFT variation of the key to the left of the 1 (tilde: ~).

The problem here is that you’re setting y to 25 in your for loop. So in each loop, even though it was incremented last loop, its getting set back to 25 again. This means each line of text is going to be drawn at 25.

Move the var y = 25; above the for loop and you should be fine.

javascript code
/* global fill, textSize, text */

var cars = {

colors : ["black", "red"],

models : ["pontiac", "cadillac"],

year : "1970"

};

fill(55, 110, 10);

textSize(20);

var y = 25;

for(cars.year = 1970; cars.year <= 1980 ; cars.year++) {

y = y + 25;

text("The " + cars.colors[0] + cars.models[0] + " is from " + cars.year + ".", 10, y);

}

the above code does exactly what is desired - 11 distinct sentences, nicely spaced, for each car year from 1970 - 1980

<< Move the var y = 25; above the for loop and you should be fine. >>

Yes - that did the trick and gave me the 11 distinct sentences that I was looking for - thanks - eventually things like this should sink in.

As to the quotes I used in -

‘’’

javascript

code

‘’’

I did not go anywhere near the extreme left of the keyboard so I don’t know how I managed to get smart quotes bracketing “javascript code”. The quotes that I did use came from the key to the left of the return key and I used the single quotes like this - ‘’’ - so I don’t know what to make of this but hopefully quotes will not be a problem in the future since they haven’t been much of an issue in the past.

You can try explicitly disabling smart quotes from your Keyboard settings on the mac (I believe you mentioned you are using an iMac):

Yes I am on an iMac - great solution to the problem even though, as I have said, my use of smart quotes usually did not result in code not running properly. Still, now that I have disabled the smart quotes, this problem should not rear its head again. Thanks for the great tip.

Keeping fingers and toes crossed :stuck_out_tongue: