Today I have sloved the problem in javascript

 Soundex is an algorithm used to categorize phonetically, such that two names that sound alike but are spelled differently have the same representation.


Soundex maps every name to a string consisting of one letter and three numbers, like M460.


One version of the algorithm is as follows:Remove consecutive consonants with the same sound (for example, change ck -> c).

Keep the first letter. The remaining steps only apply to the rest of the string.

Remove all vowels, including y, w, and h.

Replace all consonants with the following digits:

b, f, p, v -> 1

c, g, j, k, q, s, x, z -> 2

d, t -> 3

l -> 4

m, n -> 5

r -> 6If you don't have three numbers yet, append zeros until you do. Keep the first three numbers. Using this scheme, Jackson and Jaxen both map to J250.Implement Soundex

function soundex(name) {

    if (!name || typeof name !== 'string') {

        return null;

    }

name = name.toUpperCase();

let result = name.charAt(0);

name = name.replace(/[aeiouywh]/ig, '');

    name = name.replace(/([bfpv])/ig, '1');

    name = name.replace(/([cgjkqsxz])/ig, '2');

    name = name.replace(/([dt])/ig, '3');

    name = name.replace(/([l])/ig, '4');

    name = name.replace(/([mn])/ig, '5');

    name = name.replace(/([r])/ig, '6');

result += name.substr(1, 3).padEnd(3, '0');


    return result;

}

console.log(soundex("Jackson"));  

console.log(soundex("Jaxen"));    

Output:

J222
J250

Comments

Popular posts from this blog

Building a Full-Stack Student Management System with React.js and NestJS

Today I have solved the problem in javascript

Today I have solved problem in javascript