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

300
Views
Credit Card Mask kata on CodeWars - JavaScript

These are the instructions for the kata I'm working on:

Usually when you buy something, you're asked whether your credit card number, phone number or answer to your most secret question is still correct. However, since someone could look over your shoulder, you don't want that shown on your screen. Instead, we mask it.

Your task is to write a function maskify, which changes all but the last four characters into '#'.

I'm having a hard time figuring out why this code is able to print out the correct answer

let maskify = cc => console.log('#'.repeat(cc.length - 4) + cc.substr(-4));

But the following code only returns "RangeError: Invalid count value at String.repeat ()"

let maskify = cc => {
  return '#'.repeat(cc.length - 4) + cc.substr(-4);
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

How are you running your function? I ran your function in repl.it with some sample input, and it works fine.

let maskify = cc => {
  return '#'.repeat(cc.length - 4) + cc.substr(-4);
}

console.log(maskify("abcdefghijklmnop")); 
//logs "############mnop"
about 4 years ago · Juan Pablo Isaza Report

0

Both code snippets produce same results. If the parameter length is less than 4, it will error-out.

let maskify = cc => console.log('#'.repeat(cc.length - 4) + cc.substr(-4));
maskify('1234123456785678'); // Masks first 12 digits
maskify('12341'); // Masks first digit
maskify('123'); // Invalid count value error

let maskify = cc => {
  return '#'.repeat(cc.length - 4) + cc.substr(-4);
};
// Same results as previous code
console.log(maskify('1234123456785678')); // Masks first 12 digits
console.log(maskify('12341')); // Masks first digit
console.log(maskify('123')); // Invalid count error

How it works

  • First the parameter cc needs to be at least 4 chars length (typically, it may be 16 chars length)
  • The '#'.repeat() instructs that the character # needs to be repeat-ed.
  • The parameter sent to repeat tells how many times. So, cc.length - 4 indicates that for cc of 16 chars, there will be 12 '#' repeated
  • The + seeks to concatenate the repeated '#'s with the operand on its right
  • The cc.substr(-4) extracts the last 4 characters from cc (& this is the operand on the right-side of + for concatenation).

Both the snippets do the same logic. The first one logs to the console; while the second one returns the string.

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!