【问题标题】:I can't seem to figure out how to do a string interpolation; Code Platoon Exercise我似乎无法弄清楚如何进行字符串插值;代码排练习
【发布时间】: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


【解决方案1】:

您不断地重新分配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);

【讨论】:

  • 成功了,谢谢!我将仔细研究如何更好地使用联接。
猜你喜欢
  • 2021-06-04
  • 2011-07-08
  • 2016-03-12
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
  • 2017-07-12
  • 2020-11-14
  • 1970-01-01
相关资源
最近更新 更多