Javascript: Find Longest Word in a String

Javascript: find longest word in a string

That's because you're not comparing all the items in the array, you leave out the last one.

for (var i = 0; i < str.length - 1; i++)

should be

for (var i = 0; i < str.length; i++)

or

for (var i = 0; i <= str.length - 1; i++)

Find the longest word in a string using javascript

Your return statement should be outside the for loop.
It only executes the first loop then bails out.

How to find the longest word in a String within an Array in JS

Hope I understood the question correctly - otherwise let me know..

If you'll need further explanation let me know aswell

const items = ['The Soviet Union', 'The Consomol', 'United States'];

let longestWord = '';

// Loop through every item in the array
items.forEach((item) => {

// Let's split it by space ex. 'The Soviet Union' === ['The', 'Soviet', 'Union']
const words = item.split(" ");

// Loop through each word in the new array
words.forEach((word) => {
// Check if current word is longer than the one we already saved
if (word.length > longestWord.length) {
// If longer - let's update
longestWord = word
}
});
});

// All done - let's output the longest word
console.log(longestWord)

How can I find the longest words in the string and return those (excluding duplicates) along with maximum length?

function longestWord(sentence) {  // First we divide the sentence into words  var words = sentence.split(' ');    // We then get the length by getting the maximun value of the length of each word  var length = Math.max(...words.map(a=>a.length));  return {    length: length,    // Finally we filter our words array returning only those of with the same length we previously calculated    words: words.filter(i => i.length == length)  }}console.log(longestWord("The quick brown as bbbbbb fox jumped over the lazy dog"));

Find longest word in string - JavaScript

You need to remove this line in your function:

str = "This is Andela";

You function should be (added check if str is string):

function longest(str) {
if(typeof str !== 'string') return '';
var words = str.split(' ');
var longest = '';

for (var i = 0; i < words.length; i++) {
if (words[i].length > longest.length) {
longest = words[i];
}
}
return longest;
}

find longest word in a string

There you go:

function longest(str) {  var words = str.split(' ');  var longest = ''; // changed
for (var i = 0; i < words.length; i++) { if (words[i].length > longest.length) { // changed longest = words[i]; // changed } } return longest;}console.log(longest("This is Andela"));

find the longest word in a string without using the split method

Loop through each character in the string, adding to the current word. If you get to a space, check if your current word is longer than the previous longest word. If it is, store it. If it isn't, do nothing with it.

Clear out the current word and continue moving through the string.

function findLongestWordLength(str) {  let longestWord = "";  let currentWord = "";    //Move through the string letter-by-letter  for (let i = 0; i < str.length; i++) {    if (str.charAt(i) === " ") { //If we're at a space character      if (currentWord.length > longestWord.length) longestWord = currentWord; //Check if that word was the longest      currentWord = ""; //Reset the current word    } else {      currentWord += str.charAt(i); //Not at a space character, still building the current word    }  }  if (currentWord > longestWord) longestWord = currentWord; //End of string - check current word once more  return longestWord;}
const longest = findLongestWordLength("The quick brown fox jumped over the lazy dog");console.log(longest);


Related Topics



Leave a reply



Submit