JavaScript Getting Started With ES2015 Using Template Literals String Interpolation

Paul Kelly
Paul Kelly
3,541 Points

Nothing showing in the interpolation div

As far as I can see my code is identical to the code in the video on the interpolation.js file, but when I preview there is nothing in the interpolation div.

function like(thing) {
  return 'I like ' + thing;
}

function love(thing) {
  return `I love ${thing}`;
}

const sentence = `<p>` + like('apples') + ', but' love('oranges') + `.</p>`;

document.querySelector('.interpolation').innerHTML = sentence;

Andrew's code in the video is this:

alt text

I thought it would be that I need to change the like function to be the same as the new love function, but that isn't what is done in the video.

Any tips greatly appreciated, I'm completely new to any sort of programming and (mostly) enjoying myself so far :)

1 Answer

andren
andren
28,390 Points

You have a typo in this line:

const sentence = `<p>` + like('apples') + ', but' love('oranges') + `.</p>`; 

You are missing a + between the ', but' string and the love('oranges') function call. That typo will cause a syntax error which would lead your code to stop executing. If you fix that typo like this:

const sentence = `<p>` + like('apples') + ', but' + love('oranges') + `.</p>`; 

Then your code should work correctly, assuming your HTML code matches the one shown in the video.

Paul Kelly
Paul Kelly
3,541 Points

Thank you, I thought I was losing it for a moment there!