【发布时间】:2019-09-02 10:28:56
【问题描述】:
自学并坚持解决这个问题。
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);
【问题讨论】:
-
因为你在
template中的值会覆盖url,所以你需要使用不同的名字来保存不同的字符串值 -
您不断为
url分配不同的值,因此只计算最后一个值。就做console.log(`${template1}`);console.log(`${template}`);... -
目标到底是什么?让代码更干燥? “重构这段代码”不是很准确的描述
标签: javascript node.js string ecmascript-6