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.")
相关推荐
别来无恙1491 小时前
岛屿周长问题的三种解法:直接计数法、数学计算法与深度优先搜索
java·c++·算法·深度优先·dfs
UP_Continue2 小时前
排序--计数排序
数据结构·算法
牵手夏日5 小时前
题目类型——左右逢源
算法
愚润求学5 小时前
【递归、搜索与回溯】FloodFill算法(一)
c++·算法·leetcode
夜影风6 小时前
Linux系统中自签名HTTPS证书
linux·运维·https
wb1896 小时前
流编辑器sed
运维·笔记·ubuntu·云计算
sunny-ll7 小时前
【C++】详解vector二维数组的全部操作(超细图例解析!!!)
c语言·开发语言·c++·算法·面试
成工小白7 小时前
【Linux】C语言模拟实现shell命令行(程序替换原理)
linux·运维·服务器
西装没钱买8 小时前
C语言多进程TCP服务器与客户端
服务器·c语言·tcp/ip·进程
嵌入式@秋刀鱼8 小时前
《第四章-筋骨淬炼》 C++修炼生涯笔记(基础篇)数组与函数
开发语言·数据结构·c++·笔记·算法·链表·visual studio code