2024-09-16

 16 Sep 2024

Write a doubleNumbers Template Literal tag

Taken from Execute Program

Write a doubleNumbers template literal tag. It mimics normal string interpolation, but doubles all of the interpolated values as they’re inserted.

Solution

This is the answer I came up with using JavaScript:

/**
 * @param {TemplateStringsArray} strings
 * @param {number[]} values
 * @return {string}
 */

function doubleNumbers(strings, ...values) {
  let result = '';
  for (let i=0; i<strings.length; i++) {
    result += strings[i]
    if (values[i]) {
    result += (values[i] * 2).toString()
    }
  }
  return result
}

console.log(
    doubleNumbers`the numbers ${1} and ${2}` // 'the numbers 2 and 4'
)

Testing

Solution tested in JavaScript REPL:

https://www.typescriptlang.org/play/?filetype=js#code/PQKgUGDqBOCWAuBTABAQ2QAwCYHsCuARgDaIByeAtgYtAM4bJIUAORqSyRCNqRjqAcwB0yAJLxkFWFIDGtZADsc0Cr2S14cBQOSwFSaMxxt4sHAoA0YAngm5CJebz44AZowAWKPQaMnEWMgAbrx4iE7y8F4AngDk0N4KtDRIWEIQIMAQoODIIMgAAsyo0KgUyADeACqILP4Aypp6ArQAgtCl0QC+6k3atGB5hcWl5RUKlNTQANoAuj0hRGEDQwUJ8HjQCpUaWgJdg5kQrngKMqbmyPbEZJM0tAAUu820FshCH4vLAJSVg5yICQJWh4IgSAC8yFisQA3P9XMpkA8SBJYOCAAww3QAHme-SEJG0USxsAA1KTfhV-shkMDQRJSZC8S1prBZtTdO4Hl9wqzZpSOXSwchGUiebQ+UMAEzfITwHCNPYPb4cg40tW0wGbbZC+BgA5gGTmWjGRAEnACB4c64kchUe4YKIoCb2ujIAAkFQAjD1UApAp6pV0GMBgFCnYo7m6pWh-cgACyxMAqoA

Copyright © Paramdeo Singh · All Rights Reserved · Built with Jekyll

This node last updated October 9, 2024 and is permanently morphing...

Paramdeo Singh Guyana

Generalist. Edgerunner. Riding the wave of consciousness in this treacherous mortal sea.

Technology Design Strategy Literature Personal Blogs
Search Site

Results are from Blog, Link Dumps, and #99Problems