我似乎无法弄清楚如何进行字符串插值。代码排练习

时间:2019-09-02 02:24:03

标签: javascript node.js string ecmascript-6

自学成才,并坚持解决这个问题。

https://www.codeplatoon.org/intro-to-coding-session-3/

// Use string interpolation to refactor this code.

const countdownFive = "There are 5 seconds left until liftoff!"

const countdownFour = "There are 4 seconds left until liftoff!"

const countdownThree = "There are 3 seconds left until liftoff!"

const countdownTwo = "There are 2 seconds left until liftoff!"

const countdownOne = "There is 1 second left until liftoff!"

const liftOff = "Lift Off!"


我尝试了一些操作,但没有任何效果。完全来说,我是编程的新手,从字面上可以自我学习。


这是我的最新尝试,但我只得到最后一行“ Lift Off”返回,我试图将其打印出倒数型方法。

var five = "5";
var four = "4";
var three = "3";
var two = "2";
var one = "1";

var template5 = `There are ${five} seconds left until liftoff!`

var template4 = `There are ${four} seconds left until liftoff!`

var template3 = `There are ${three} seconds left until liftoff!`

var template2 = `There are ${two} seconds left until liftoff!`

var template1 = `There are ${one} seconds left until liftoff!`

var template = `Lift Off!`

var url = `${template5}`;

var url = `${template4}`;

var url = `${template3}`;

var url = `${template2}`;

var url = `${template1}`;

var url = `${template}`;

console.log(url);

1 个答案:

答案 0 :(得分:0)

您一直在重新分配url-在以下两者之间登录:

var five = "5";
var four = "4";
var three = "3";
var two = "2";
var one = "1";

var template5 = `There are ${five} seconds left until liftoff!`

var template4 = `There are ${four} seconds left until liftoff!`

var template3 = `There are ${three} seconds left until liftoff!`

var template2 = `There are ${two} seconds left until liftoff!`

var template1 = `There are ${one} seconds left until liftoff!`

var template = `Lift Off!`

var url = `${template5}`;

console.log(url);

var url = `${template4}`;

console.log(url);

var url = `${template3}`;

console.log(url);

var url = `${template2}`;

console.log(url);

var url = `${template1}`;

console.log(url);

var url = `${template}`;

console.log(url);

或使用join(这将在一条语句中多行显示):

var five = "5";
var four = "4";
var three = "3";
var two = "2";
var one = "1";

var template5 = `There are ${five} seconds left until liftoff!`

var template4 = `There are ${four} seconds left until liftoff!`

var template3 = `There are ${three} seconds left until liftoff!`

var template2 = `There are ${two} seconds left until liftoff!`

var template1 = `There are ${one} seconds left until liftoff!`

var template = `Lift Off!`

var url = [template5, template4, template3, template2, template1, template].join("\n");

console.log(url);