Skip to content

Instantly share code, notes, and snippets.

@ChrisRoss5
Created November 3, 2021 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisRoss5/1250176f2c73fee8b49b0433d4e6db2a to your computer and use it in GitHub Desktop.
Save ChrisRoss5/1250176f2c73fee8b49b0433d4e6db2a to your computer and use it in GitHub Desktop.
function z1(l: number[]) {
if (!l.length) return [];
return [
l.reduce((a, b) => a + (b > 0 ? b : 0), 0),
l.reduce((a, b) => a + (b < 0 ? b : 0), 0),
];
}
function z2(s: string) {
return [...new Set(s.split(" "))].join(" ");
}
function z3(s: string) {
const letters = "abcčćddžđefghijkljmnjoprsštuvzž";
const words = s.split(new RegExp(`[^${letters}]`, "g"));
const scores = words.map((word) =>
[...word].reduce((a, b) => a + letters.indexOf(b) + 1, 0)
);
return words[scores.indexOf(Math.max(...scores))];
}
console.log(z1([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15]));
console.log(z2("alpha beta gamma delta alpha beta beta gamma delta"));
console.log(z3("abc-def.ghi uvzž,jklmn+žzvu"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment