Write a function that finds and returns the longest word in a given sentence. If there are multiple words with the maximum length, return the first one.
Input: str = "Hey its me aman pareek"
Output: "pareek"
Input: num = 874156
Output: Throw error => Your input is not a string
function findLongestWordUsingSplitMapSort(sentence) {
if (typeof sentence !== 'string') {
throw new Error("Your input is not a string");
}
return sentence
.split(" ")
.map(word => ({ length: word.length, word }))
.sort((a, b) => b.length - a.length)[0].word;
}
const str1 = "Hey its me aman pareek";
findLongestWordUsingSplitMapSort(str1); //output: pareek
const num2 = 874156;
findLongestWordUsingSplitMapSort(num2); //Throw error => Your input is not a string
function findLongestWordUsingSplitMapFilter(sentence) {
if (typeof sentence !== 'string') {
throw new Error("Your input is not a string");
}
const words = sentence.split(" ");
const maxLength = Math.max(...words.map(word => word.length));
return words.filter(word => word.length === maxLength)[0];
}
const str1 = "Hey its me aman pareek";
findLongestWordUsingSplitMapFilter(str1); //output: pareek
const num2 = 874156;
findLongestWordUsingSplitMapFilter(num2); //Throw error => Your input is not a string
function findLongestWordUsingSplitAndSort(sentence) {
if (typeof sentence !== 'string') {
throw new Error("Your input is not a string");
}
return sentence.split(" ").sort((a, b) => b.length - a.length)[0];
}
const str1 = "Hey its me aman pareek";
findLongestWordUsingSplitAndSort(str1); //output: pareek
const num2 = 874156;
findLongestWordUsingSplitAndSort(num2); //Throw error => Your input is not a string
function findLongestWordUsingForLoop(sentence) {
if (typeof sentence !== 'string') {
throw new Error("Your input is not a string");
}
const words = sentence.split(" ");
let longestWord = "";
for (const word of words) {
if (word.length > longestWord.length) {
longestWord = word;
}
}
return longestWord;
}
const str1 = "Hey its me aman pareek";
findLongestWordUsingForLoop(str1); //output: pareek
const num2 = 874156;
findLongestWordUsingForLoop(num2); //Throw error => Your input is not a string
function findLongestWordUsingReduce(sentence) {
if (typeof sentence !== 'string') {
throw new Error("Your input is not a string");
}
return sentence.split(" ").reduce((longest, word) =>
word.length > longest.length ? word : longest, ""
);
}
const str1 = "Hey its me aman pareek";
findLongestWordUsingReduce(str1); //output: pareek
const num2 = 874156;
findLongestWordUsingReduce(num2); //Throw error => Your input is not a string
function findLongestWordUsingEvery(sentence) {
if (typeof sentence !== 'string') {
throw new Error("Your input is not a string");
}
const words = sentence.split(" ");
let longestWord = words[0];
words.every(word => {
if (word.length > longestWord.length) {
longestWord = word;
}
return true; // Continue iteration
});
return longestWord;
}
const str1 = "Hey its me aman pareek";
findLongestWordUsingEvery(str1); //output: pareek
const num2 = 874156;
findLongestWordUsingEvery(num2); //Throw error => Your input is not a string
function findLongestWordUsingForOf(sentence) {
if (typeof sentence !== 'string') {
throw new Error("Your input is not a string");
}
const words = sentence.split(" ");
let longestWord = "";
for (const word of words) {
if (word.length > longestWord.length) {
longestWord = word;
}
}
return longestWord;
}
const str1 = "Hey its me aman pareek";
findLongestWordUsingForOf(str1); //output: pareek
const num2 = 874156;
findLongestWordUsingForOf(num2); //Throw error => Your input is not a string
function findLongestWordUsingMatch(sentence) {
if (typeof sentence !== 'string') {
throw new Error("Your input is not a string");
}
const words = sentence.match(/\w+/g) || [];
return words.reduce((longest, word) =>
word.length > longest.length ? word : longest, ""
);
}
const str1 = "Hey its me aman pareek";
findLongestWordUsingMatch(str1); //output: pareek
const num2 = 874156;
findLongestWordUsingMatch(num2); //Throw error => Your input is not a string
function findLongestWordUsingSplitFilter(sentence) {
if (typeof sentence !== "string") {
throw new Error("Your input is not a string");
}
const words = sentence.split(" ");
const maxLength = words.reduce((max, word) => Math.max(max, word.length), 0);
return words.filter((word) => word.length === maxLength)[0];
}
const str1 = "Hey its me aman pareek";
findLongestWordUsingSplitFilter(str1); //output: pareek
const num2 = 874156;
findLongestWordUsingSplitFilter(num2); //Throw error => Your input is not a string
function findLongestWordUsingFind(sentence) {
if (typeof sentence !== 'string') {
throw new Error("Your input is not a string");
}
const words = sentence.split(" ");
const maxLength = Math.max(...words.map(word => word.length));
return words.find(word => word.length === maxLength);
}
const str1 = "Hey its me aman pareek";
findLongestWordUsingFind(str1); //output: pareek
const num2 = 874156;
findLongestWordUsingFind(num2); //Throw error => Your input is not a string