jsx
const findMostFrequentWord = (text) => {
const words = text.split(" ");
const wordCount = words.reduce((acc, word) => {
acc[word] = (acc[word] || 0) + 1;
return acc;
}, {});
let mostFrequentWord = "";
let maxCount = 0;
for (const word in wordCount) {
if (wordCount[word] > maxCount) {
mostFrequentWord = word;
maxCount = wordCount[word];
}
}
return mostFrequentWord;
};
console.log(findMostFrequentWord("hello world hello world hello"));
console.log(findMostFrequentWord("apple banana apple orange orange orange"));
console.log(findMostFrequentWord("one two three two three three"));
tsx
const years = Array.from({ length: 20 }, (_, i) => i + 2020);
const findMissingNumbers = (numbers: number[]) => {
const missingNumbers: number[] = [];
for (let i = 0; i < numbers.length - 1; i++) {
let current = numbers[i];
const next = numbers[i + 1];
while (current + 1 < next) {
current++;
missingNumbers.push(current);
}
}
return missingNumbers;
};
console.log(years);
console.log(findMissingNumbers([1, 2, 4, 6, 12, 15]));
tsx
const isPalindrome = (str: string) => {
const cleanedStr = str.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
const reversedStr = cleanedStr.split("").reverse().join("");
return \`\${str} is \${cleanedStr === reversedStr ? "" : "not "}a palindrome\`;
};
console.log(isPalindrome("radar"));
console.log(isPalindrome("hello"));