본문 바로가기
알고리즘/Codewars

[Codewars] Fix My Phone Numbers! (7 kyu) / JavaScript

by fluss 2023. 5. 2.

https://www.codewars.com/kata/596343a24489a8b2a00000a2

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Fix My Phone Numbers

Oh thank goodness you're here! The last intern has completely ruined everything!

All of our customer's phone numbers have been scrambled, and we need those phone numbers to annoy them with endless sales calls!

 

The Format

Phone numbers are stored as strings and comprise 11 digits, eg '02078834982' and must always start with a 0.

However, something strange has happened and now all of the phone numbers contain lots of random characters, whitespace and some are not phone numbers at all!

For example, '02078834982' has somehow become 'efRFS:)0207ERGQREG88349F82!' and there are lots more lines that we need to check.

 

The Task

Given a string, you must decide whether or not it contains a valid phone number. If it does, return the corrected phone number as a string ie. '02078834982' with no whitespace or special characters, else return "Not a phone number".

 

코드

function isItANum(str) {
  num = str.replace(/[^0-9]/g, "");
  return /^0+\d{10}$/.test(num) ? num : "Not a phone number";
}

댓글