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

110
Views
Ensure minimum string length

I want to ensure that I get a minimum length of 2 otherwise it should be filled with " ". For example:

let string = "  "
string.unknownMethod("a")

What function do I need in place of unknown method to get this output " a". But if string.unknownMethod("ab") the output should be "ab"

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

0

If what you want is to ensure that you get a string with a minimum length of 2 characters and, otherwise, get it filled with leading spaces, it's easier if you reverse your current logic:

console.log("".padStart(2, '-'));
console.log("a".padStart(2, '-'));
console.log("ab".padStart(2, '-'));
console.log("abc".padStart(2, '-'));

Spaces replaced with - for visibility.

Otherwise, you need to mess with substring offsets or regular expressions. It's doable but not as immediate.

Reference for padStart()

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!