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.")
相关推荐
小夏子_riotous11 分钟前
Docker学习路径——3、常用命令
linux·运维·服务器·学习·docker·容器·centos
墨尘笔尖1 小时前
最大最小值降采样算法的优化
c++·算法
IMPYLH3 小时前
Linux 的 rm 命令
linux·运维·服务器·网络·bash
white-persist3 小时前
【vulhub shiro 漏洞复现】vulhub shiro CVE-2016-4437 Shiro反序列化漏洞复现详细分析解释
运维·服务器·网络·python·算法·安全·web安全
FL16238631294 小时前
基于C#winform部署软前景分割DAViD算法的onnx模型实现前景分割
开发语言·算法·c#
代码中介商4 小时前
手把手教你Linux 打包压缩与 gcc 编译详解
linux·运维·服务器·编译·打包·压缩
longerxin20204 小时前
阿里云AlmaLinux操作系统允许root登录配置步骤
linux·服务器·阿里云
xuanwenchao4 小时前
ROS2学习笔记 - 2、类的继承及使用
服务器·笔记·学习
baizhigangqw4 小时前
启发式算法WebApp实验室:从搜索策略到群体智能的能力进阶
算法·启发式算法·web app
HYNuyoah4 小时前
docker 安装win10系统
运维·docker·容器