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

266
Views
Codewars: how can i solve this codewar question?

I have attempted this so many times even using regex and the len, left, mid and right methods and i cant seem to get it right. The goal is this aaa33aa4a7a6 ==> aaa33aa-a-a-

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

0

You could check if there comes at least two or less digits in the string and replace the digits.

const
    regex = /\d(?=(\D*\d){0,2}\D*$)/g;

console.log('abc12aa3b9c8');
console.log('abc12aa3b9c8'.replace(regex, '-'));

console.log('abc12aa3b9c8x');
console.log('abc12aa3b9c8x'.replace(regex, '-'));

about 4 years ago · Juan Pablo Isaza Report

0

I convert the string to an array the reverse it. Afterwards i iterate this new array and create a new array by condition (count the matches of numbers).

const str = `abc12aa3b9c8`
let arr = str.split("").reverse(); 

let counter = 0;
let newStr = []

arr.forEach(e => {  
  if (isNaN(e)) {       
    newStr.push(e)
  } else {    
    newStr.push(counter < 2 ? '-' : e)         
    counter++;
  }
})


console.log('res',newStr.reverse().join(""))

about 4 years ago · Juan Pablo Isaza Report

0

Split your string to array, reverse, replace what you need, reverse it back, join into a result string:

    var str = 'abc12aa3b9c8';
    var counter = 3;
    str = str
      .split('')
      .reverse()
      .map(function(char){
        if( !isNaN( char ) && counter-- > 0 )
          return '-';
        return char
      })
      .reverse()
      .join('');
    console.log( str );
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!