)
}
}
)
(
}
{
)
)
(
)
(
(
{
}
)
(
)
}
)
)
{
(
(
)
)
}
)
(
}

Soundex

  1. // Minimum length of Soundex keys.
  2. var minLength = 4
  3.  
  4. // Soundex values belonging to characters.
  5. // This map also includes vowels (with a value of 0) to easily distinguish
  6. // between an unknown value or a vowel.
  7. var map = {}
  8.  
  9. map.a = map.e = map.i = map.o = map.u = map.y = 0
  10. map.b = map.f = map.p = map.v = 1
  11. map.c = map.g = map.j = map.k = map.q = map.s = map.x = map.z = 2
  12. map.d = map.t = 3
  13. map.l = 4
  14. map.m = map.n = 5
  15. map.r = 6
  16.  
  17. /**
  18.  * Get the soundex key from a given value.
  19.  *
  20.  * @param {string} value
  21.  * @param {number} [maxLength=4]
  22.  * @returns {string}
  23.  */
  24. function soundex(value, maxLength) {
  25.   var lowercase = String(value).toLowerCase()
  26.   /** @type {Array.<string|number>} */
  27.   var results = []
  28.   var index = -1
  29.   /** @type {string} */
  30.   var character
  31.   /** @type {number} */
  32.   var previous
  33.   /** @type {number} */
  34.   var phonetics
  35.  
  36.   while (++index < lowercase.length) {
  37.     character = lowercase.charAt(index)
  38.     phonetics = map[character]
  39.  
  40.     if (index === 0) {
  41.       // Initial letter
  42.       results.push(character.toUpperCase())
  43.     } else if (phonetics && phonetics !== previous) {
  44.       // Phonetics value
  45.       results.push(phonetics)
  46.     } else if (phonetics === 0) {
  47.       // Vowel
  48.       phonetics = null
  49.     } else {
  50.       // Unknown character (including H and W)
  51.       phonetics = previous
  52.     }
  53.  
  54.     previous = phonetics
  55.   }
  56.  
  57.   return pad(results.join('')).slice(0, maxLength || minLength)
  58. }
  59.  
  60. /**
  61.  * Pad a given value with zero characters. The function only pads four characters.
  62.  *
  63.  * @param {string} value
  64.  * @returns {string}
  65.  */
  66. function pad(value) {
  67.   var length = minLength - value.length
  68.   var index = -1
  69.  
  70.   while (++index < length) {
  71.     value += '0'
  72.   }
  73.  
  74.   return value
  75. }
  76.  
  77. console.log(
  78.   soundex('Joe'),
  79.   soundex('Smith'),
  80.   soundex('Ed'), 
  81.   soundex('Vilowat')
  82. )

Was talking with someone about Soundex… I grabbed this implementation from the soundex-code npm package from Titus Wormer

Soundex has always been super interesting to me for some reason.

charAt

  1. '0123'.charAt('1') // -> '1'
  2. '0123'.charAt(1.45) // -> '1'
  3. '0123'.charAt(true) // -> '1'
  4. '0123'.charAt() // -> '0'
  5. '0123'.charAt(NaN) // -> '0'
  6. '0123'.charAt(-1) // -> ''
  7.  
  8. Number.prototype.charAt = String.prototype.charAt
  9. NaN.charAt() // -> 'N'
  10.  
  11. Boolean.prototype.charAt = Boolean.prototype.charAt
  12. true.charAt() // -> t

Funny snippet from Räphael’s creator Dmitry Baranovskiy’s talk Zen of JavaScript

Here is the part where he talks about this:

Regex Match Words and More

  1. const phrase = `"That's the password: 'PASSWORD 123'!", cried the Special Agent.\nSo I fled.`;
  2. const words = phrase.toLocaleLowerCase()
  3.   .replace(/["!.:,]/g, '')
  4.   .split(/\s\'|\'\s|\n|\s/g);
  5.   console.log(words);

Recently did this for a stackoverflow question… check it out…

There are other great (better) answers like this one by use MMMahdy-PAPION:

  1. const phrase = `"That's the password: 'PASSWORD 123'!", cried the Special Agent.\nSo I fled.`
  2. console.log(
  3.   phrase.toLocaleLowerCase().match(/(?!')[\w']*\w/g)
  4. );

toString Radix

  1. for (let i = 2; i < 36; i++) {
  2.   console.log((234).toString(i), ` = 234 in base ${i}`)
  3. }

Use the radix argument of toString

Hamming Distance in JavaScript

  1. function hamming(a, b) {
  2.   const leng = a.length
  3.   let dist = 0
  4.  
  5.   // strings need to be same length
  6.   if (leng != b.length) return -1;
  7.  
  8.   a = a.toLowerCase()
  9.   b = b.toLowerCase()
  10.  
  11.   for (let i = 0; i < leng; i++)
  12.     if (a[i] !== b[i]) dist++
  13.  
  14.   return dist
  15. }
  16.  
  17. console.log(hamming('zevan', 'kevin'))
  18. console.log(hamming('joe', 'joe'))
  19. console.log(hamming('john', 'jake'))

The hamming distance between two strings…

snippet.zone ~ 2021-24 /// {s/z}