Clone and edit this document
Runkit
Runkit
home page
user forum
new notebook
clone notebook
download notebook
support & documentation
log in
sign up
new notebook
help & feedback
clone this notebook
download this notebook
Sign In
Sign Up
RunKit + npm: js-levenshtein
node v15.14.0
version:
1.0.0
endpoint
share
tweet
const jsLevenshtein = require("js-levenshtein"); function closestCommand (userText, commands) { let minDistance = Infinity; return commands.reduce((closest, cmd) => { console.log(`closest: ${closest}, cmd: ${cmd}`); const cmdDistance = jsLevenshtein(userText, cmd); if (cmdDistance < minDistance) { minDistance = cmdDistance; return cmd; } return closest; }, ''); } const myCommands = ['quit', 'login', 'logout', 'restart', 'refresh']; const userCommand = 'restrat'; console.log(`closest command is '${closestCommand(userCommand, myCommands)}'`);
Created from: https://npm.runkit.com/js-levenshtein
Loading…
no comments
sign in
to comment