JSFiddle - React, Tailwind, and code Playground
HTML
<script src="//wzrd.in/standalone/expect@latest"></script>
<script src="//wzrd.in/standalone/deep-freeze@latest"></script>
<body>
</body>
Babel + JSX
const distance = (string1, string2) => {
if (!string1 || string1.length === 0) {
return string2.length;
}
if (!string2 || string2.length === 0) {
return string1.length;
}
let [head1, ...tail1] = string1;
let [head2, ...tail2] = string2;
if (head1 === head2) {
return distance(tail1, tail2);
}
const l1 = distance(string1, tail2);
const l2 = distance(tail1, string2);
const l3 = distance(tail1, tail2);
return 1 + Math.min(l1, l2, l3);
}
const test = () => {
expect(distance('harry', 'harry'))
.toEqual(0);
expect(distance('harry', ''))
.toEqual(5);
expect(distance('harry', 'hairy'))
.toEqual(1);
expect(distance('harry', 'hry'))
.toEqual(2);
};
const logdisp = (arg) => {
document.querySelector('body').innerHTML = arg;
console.log(arg)
};
test();
logdisp('All tests passed');