0061【Edabit ★☆☆☆☆☆】Format I: Template String

0061【Edabit ★☆☆☆☆☆】Format I: Template String

language_fundamentals strings

Instructions

Write a template string according to the following example:

Examples
javascript 复制代码
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:

javascript 复制代码
const name = John;
`hello, my name is ${name}.` // "hello, my name is John."

You can put an expression inside the curly braces :

javascript 复制代码
const age = 12;
`Hello, you are ${age < 18 ? 'young' : 'old'}.` // "Hello, you are young."
Notes
  • N/A
Solutions
javascript 复制代码
// 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 = "your template string"
    const template = `Their names were: ${a}, ${b} and ${c}.`
    return template
}
TestCases
javascript 复制代码
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.")
相关推荐
.YM.Z13 分钟前
【数据结构】:排序(二)——归并与计数排序详解
数据结构·算法·排序
武帝为此14 分钟前
【数据结构之树状数组】
数据结构·算法
失败才是人生常态15 分钟前
算法题归类学习
学习·算法
leoufung31 分钟前
用 DFS 拓扑排序吃透 LeetCode 210:Course Schedule II
算法·leetcode·深度优先
❀͜͡傀儡师33 分钟前
docker部署 paperless-ngx
运维·docker·容器
谷粒.34 分钟前
测试数据管理难题的7种破解方案
运维·开发语言·网络·人工智能·python
卌卄37 分钟前
Linux下安装Docker
linux·运维·docker
chao18984440 分钟前
电容层析成像Tikhonov算法
算法
会挠头但不秃40 分钟前
2.逻辑回归模型
算法·机器学习·逻辑回归
派阿喵搞电子41 分钟前
配置srs的鉴权时遇到的问题
服务器·docker·容器·srs