Day 83: Longest Word From a String of 100DaysCodingChallenge
Hello all,
#day83
Day83
Today's task was to Write a JavaScript program to find the largest word in a given string. Words must be separated by only one space.
Example:
Sample Input: JavaScript code
Sample Output: The Largest Word is JavaScript
I have started the 100 days of coding challenge to improve my programming skills..
Code :
function getLongestWord(str) {
let words = str.split(' ');
let maxLength = 0;
let longestWord = '';
for (let i = 0; i < words.length; i++) {
if (words[i].length > maxLength) {
maxLength = words[i].length;
longestWord = words[i];
}
}
//console.log(maxLength);
console.log("The Longest Word is ", longestWord);
}
let str = prompt("Enter a String : ");
getLongestWord(str);
Output :
Happy Coding:)
Thank you...

Comments
Post a Comment