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.")
相关推荐
cg_ssh29 分钟前
Docker 下启动 Nacos 3.1.1 单机模式
运维·docker·容器
修己xj32 分钟前
使用 Docker 部署 SQL Server 并导入 .mdb 文件的完整指南
运维·docker·容器
你撅嘴真丑1 小时前
字符环 与 变换的矩阵
算法
早点睡觉好了1 小时前
重排序 (Re-ranking) 算法详解
算法·ai·rag
gihigo19981 小时前
基于全局自适应动态规划(GADP)的MATLAB实现方案
算法
郝亚军2 小时前
ubuntu-18.04.6-desktop-amd64安装步骤
linux·运维·ubuntu
Web极客码2 小时前
CentOS 7.x如何快速升级到CentOS 7.9
linux·运维·centos
ctyshr2 小时前
C++编译期数学计算
开发语言·c++·算法
zh_xuan3 小时前
最小跳跃次数
数据结构·算法
一位赵3 小时前
小练2 选择题
linux·运维·windows