JSFiddle - React, Tailwind, and code Playground
by nsatija
JavaScript
const alphabet = { c: 1, d: 2, a: 3, b: 4, n: 5 };
//Now that we know how to store our custom alphabet, implement a function that takes in two words and any made-up custom alphabet //object and returns the word that comes first in the alphabet's dictionary. To save time, I've provided an example alphabet in //the boilerplate below.
const compare = (a, b, dict) => {
var sorted = [];
for(var key in dict) {
sorted[sorted.length] = key;
}
sorted.sort();
var tempDict = {};
for(var i = 0; i < sorted.length; i++) {
tempDict[sorted[i]] = dict[sorted[i]];
}
console.log(tempDict)
};
compare('','',alphabet)
//console.log(compare("banda", "bandcna", alphabet)); // bandcna
//console.log(compare("banda", "bandn", alphabet)); // banda
//console.log(compare("ban", "ban", alphabet)); // ban