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.")
相关推荐
Zww089117 分钟前
docker部署个人网页导航
运维·docker·容器
运维小白。。19 分钟前
Nginx 反向代理
运维·服务器·nginx·http
PeterJXL20 分钟前
Docker-compose:管理多个容器
运维·docker·容器
海王正在撒网24 分钟前
用 Docker 部署 Seafile 社区版
运维·docker·容器
科技互联人生26 分钟前
中国数据中心服务器CPU行业发展概述
服务器·硬件架构
luthane1 小时前
python 实现average mean平均数算法
开发语言·python·算法
xuanyu221 小时前
Linux常用指令
linux·运维·人工智能
静心问道1 小时前
WGAN算法
深度学习·算法·机器学习
KookeeyLena52 小时前
云手机可以挂在服务器使用吗?
运维·服务器·智能手机
杰九2 小时前
【算法题】46. 全排列-力扣(LeetCode)
算法·leetcode·深度优先·剪枝