-1

I have to write a unit test for a JavaScript function to compare two phone numbers. This is a function whose input parameters are two strings and the return type is true / false. True if the phone numbers are the same, false if they are not. As many cases as possible should be included in the test.

Definition of function is phoneNrsAreEqual(nr1: String, nr2: String): Bool

Thanks for helping me!

Simon Thiel
  • 2,888
  • 6
  • 24
  • 46
kerpi
  • 1
  • 1

2 Answers2

0

I believe this is what you need?

You can remove spaces with the replace function.

function phoneNrsAreEqual(firstNumber, secondNumber) {
    
    return removeSpaces(firstNumber) == removeSpaces(secondNumber);

}

function removeSpaces(str) {
    
    return str = str.replace(/\s+/g, '');

}
  • yes i am looking for something like these but i have to consider that the numbers can be with space or with for example 0947854789 and 0947 854 789 numbers are same but it will return false because of space or if use pre dial. – kerpi Nov 06 '19 at 11:23
  • 1
    `return firstNumber == secondNumber` returns true or false, the use of `? true : false` is pointless. – Austin France Nov 06 '19 at 11:30
  • 1
    `return str = str.replace(/\s+/g,'');` should really be just `return str.replace(/\s+/g,'');` the assignment in the return is pointless. – Austin France Nov 08 '19 at 14:04
0

The place to start would be to clean the phone numbers by creating versions of the strings without punctuation.

function cleanPhoneNumber(n) {
  return n.replace(/[() +-]/g,"");
}

function phoneNrsAreEqual(nr1, nr2) {
  return cleanPhoneNumber(nr1) === cleanPhoneNumber(nr2);
}

It gets much more complicated if the numbers include international numbers as there are so many different ways of representing them.

(01234) 567890
+44 (1234) 567890
01144 (1234) 567890   (when dialed from US)

are all the same number.

Austin France
  • 2,381
  • 4
  • 25
  • 37
  • Thanks for your help austin. So if i understand it right it works that the function cleanPhoneNumber is calles in functin to compare numbers ? – kerpi Nov 06 '19 at 11:35
  • Yes. `cleanPhoneNumber` is a helper function, given a phone number, returns a version of that number with punctuation removed. The return statement is comparing a cleaned version of `nr1` with a cleaned version of `nr2`. The `===` operator does a strict equality comparison to check if the clean strings are equal yielding true or false. – Austin France Nov 06 '19 at 11:43