0061【Edabit ★☆☆☆☆☆】【字符串模板:格式化1】Format I: Template String

编程入门 行业动态 更新时间:2024-10-10 10:25:05

0061【Edabit ★☆☆☆☆☆】【<a href=https://www.elefans.com/category/jswz/34/1771434.html style=字符串模板:格式化1】Format I: Template String"/>

0061【Edabit ★☆☆☆☆☆】【字符串模板:格式化1】Format I: Template String

0061【Edabit ★☆☆☆☆☆】【字符串模板:格式化1】Format I: Template String

language_fundamentals strings

Instructions

Write a template string according to the following example:

Examples
const a = "John";
const b = "Joe";
const c = "Jack";
const template = "your template string" // "Their names were:  John,  Joe  and  Jack."

Tips
A template string is a string that uses a Dollar sign and curly braces inside backticks ${} as a placeholder that can then be formatted:

const name = John;
`hello, my name is ${name}.` // "hello, my name is John."

You can put an expression inside the curly braces :

const age = 12;
`Hello, you are ${age < 18 ? 'young' : 'old'}.` // "Hello, you are young."
Notes
  • N/A
Solutions

// modify the template variable to be a template string 
function format(a, b, c) {
// the result string must give: "Their names were: a, b and c."const template = `Their names were: ${a}, ${b} and ${c}.`return template
}
TestCases
let Test = (function(){return {assertEquals:function(actual,expected){if(actual !== expected){let errorMsg = `actual is ${actual},${expected} is expected`;throw new Error(errorMsg);}},assertSimilar:function(actual,expected){if(actual.length != expected.length){throw new Error(`length is not equals, ${actual},${expected}`);}for(let a of actual){if(!expected.includes(a)){throw new Error(`missing ${a}`);}}}}
})();Test.assertEquals(format("John", "Joe", "Jack"), "Their names were: John, Joe and Jack.")
Test.assertEquals(format("Peter", "Pin", "Pan"), "Their names were: Peter, Pin and Pan.")
Test.assertEquals(format("E", "Da", "Bit"), "Their names were: E, Da and Bit.")
Test.assertEquals(format("Bulbasaur", "Charmander", "Squirtle"), "Their names were: Bulbasaur, Charmander and Squirtle.")

更多推荐

0061【Edabit ★☆☆☆☆☆】【字符串模板:格式化1】Format I: Template String

本文发布于:2023-11-17 05:48:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   模板   Edabit   Template   Format

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!