Interpolation

Hello,

I have a problem when I do JS exercises with interpolations: I type ${expression} and the system does not accept this. When I copy paste ${expression} from Codeacademy website it works fine. The problem is there is not always ${expression} provided in the solution so I cannot copy paste it. Example is a ‘Race Day’ project. Could you please advise how I can fix that?

https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-control-flow/projects/race-day

Can you give a specific example from your code? Press </> before pasting as seen here —> Screen Shot 2020-11-06 at 9.11.19 AM

2 Likes

Hello,

Sure, here is a code example, I was referring specfically to ${raceNumber}

if(early && age > 18){
 console.log('Race will begin at 9:30 AM, your race number is: ${raceNumber}.');
}

The complete code:

let raceNumber = Math.floor(Math.random() * 1000);

const registeredEarly = false;
const early = false;
const age = 18;

if(early && age > 18) {
  raceNumber += 1000;
}

if(early && age > 18){
 console.log('Race will begin at 9:30 AM, your race number is: ${raceNumber}.');
}

else if(!early && age > 18)  {
  console.log('Late adults run at 11:00 AM, your race number is: ${raceNumber}.');
}

else if(age < 18) {
  console.log('Youth registrants run at 12:30 PM, your race number is: ${raceNumber}.');
}

else{
  console.log('Please go to the registration desk.');
}

In order to use string interpolation, you’ll need to use the backtick instead of a single or double quote mark. The difference can be difficult to see at first:

// Incorrect
console.log('Race will begin at 9:30 AM, your race number is: ${raceNumber}.');

// Correct
console.log(`Race will begin at 9:30 AM, your race number is: ${raceNumber}.`);
2 Likes

Great, it works, thanks!