I'm trying to implement a function which checks whether a list of letters are all present in a word.
function wordHasLatters(word,letters){ let wordChars={}; word.split("").map(c=>wordChars[c]=true); let ok=true; letters.split("").map(l=>ok=ok&&(wordChars[l]!=undefined)); return ok; } It is all relatively elegant with using maps. What bothers me is that I cannot return from the second map if I detect that a letter is not present. So I have to use a variable and return this. This is two extra lines in the code.
Is there a way to optimize this code?
.map()and ignoring its result. This is either a case for.reduce()or for imperative loops. Using.map()is just so odd here..map()are incorrect..map()should only be used to convert one array to another array of the same length, by returning an array element value in each iteration, and then assigning the result of.map()to a variable.