Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
leave the original letter if can't find one to replace

I'm trying to transform a message with default font to the font "vaporwave", but when it finds a letter that isn't defined, it returns undefined, how could i make it skip that letter that wasn't defined and leave the original one?

font = {
    "A": "A",
    "B": "B",
    "C": "C",
    "D": "D",
    "E": "E",
    "F": "F",
    [...]
}

const string = "hello world"
const vapour = string.split('').map(letter => {
  return font[letter];
}).join('');

//return for example, "undefined EL L O [...]"
//i want: "h EL L O [...]"


about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can alternate the font[letter] with the original letter:

const vapour = string.split('').map(letter => {
  return font[letter] || letter;
}).join('');

Or, what I'd prefer would be to construct a regular expression from the keys of the object:

const pattern = new RegExp('[' + Object.keys(font).join('') + ']', 'g');
const vapour = string.replace(pattern, char => font[char]);
about 4 years ago · Juan Pablo Isaza Report

0

Use Nullish coalescing operator (??) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator

const vapour = string.split('').map(letter => {
  return font[letter] ?? letter;
}).join('');
about 4 years ago · Juan Pablo Isaza Report

0

Why not just check if the letter is present in the dictionary ?

font = {
    "A": "A",
    "B": "B",
    "C": "C",
    "D": "D",
    "E": "E",
    "F": "F",
}

const string = "ABCDEFG123"
const vapour = string.split('').map(letter => {
  if (letter in font) {
    return font[letter];
  } else {
    return letter
  }
}).join('');
console.log(string, "-->", vapour);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!