Javascript: find longest word in a string

find the longest word in an array javascript
find the longest word in a string java
find the longest word in a string python
find the longest word in a string in c
modify the function to return the longest string in the given array.
basic algorithm scripting: find the longest word in a string
find longest word in string c#
how to find longest word in a sentence
function longestWord(string) {
    var str = string.split(" ");
    var longest = 0;
    var word = null;
    for (var i = 0; i < str.length - 1; i++) {
        if (longest < str[i].length) {
            longest = str[i].length;
            word = str[i];
        }
    }
    return word;
}

When I call longestWord("Pride and Prejudice"), it returns 'Pride' and not 'Prejudice' which is the longest word... why? I checked some other similar questions, but the solutions looked a lot like my code.


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++)

JavaScript function: Find the longest word within a string, A tape measure. Not very useful for measuring the lengths of words. I'm a non-​professional, self-taught web developer. I decided to start  Find the Longest Word With the reduce() Method For this solution, we will use the Array.prototype.reduce(). The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.


One advantage to taking a functional approach to such problems is that you don't even have to keep count:

function longer(champ, contender) {
    return (contender.length > champ.length) ? contender : champ;
}

function longestWord(str) {
    var words = str.split(' ');
    return words.reduce(longer);
}

See MDN Array.reduce for more info. (note: reduce needs shim for IE8)

How to find the longest word in a string using JavaScript, 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;  And in the for, assign the string having the length greater than the current longest string to it. At the end of the function, you can return the longest string. Problems/Suggestions: Use str.split(' ') to directly assignment to words variable; Use longest as string variable instead of array and initialize it to empty string, i.e. '', to avoid the above error


You have a -1 in your condition, it never even scans it:

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

Should be:

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

Demo: http://jsfiddle.net/LfgFk/

Javascript: find longest word in a string, This is a classic algorithm scripting challenge. Pluck out the longest word in a string! It's easy for us to do with our brains, but how are we going to tell the  In this one, I’m going to show you two ways to tackle the 4th Basic Algorithm Scripting challenge Find the Longest Word in a String. The challenge is to return the length of the longest word in a sentence (as a number). As with most things in coding there is more than one way to skin a cat. There is no right way or wrong way.


Here this is your solution with a forEach, this will help you avoid the error in the future

function longestWord(string) {
    var str = string.split(" ");
    var longest = 0;
    var word = null;
    str.forEach(function(str) {
        if (longest < str.length) {
            longest = str.length;
            word = str;
        }
    });
    return word;
}
console.log(longestWord("pride and prejudice"));

Your original problem was just the str.length - 1 should have just been str.length, originally you wouldn't have gotten to the last element of the array

Find the longest word in a string in JavaScript - Dylan Attal, This article is based on Free Code Camp Basic Algorithm Scripting “Find the Longest Word in a String”. In this algorithm, we want to look at each individual word  Previous: Write a JavaScript function that accepts a string as a parameter and converts the first letter of each word of the string in upper case. Next: Write a JavaScript function that accepts a string as a parameter and counts the number of vowels within the string.


The index is going up to str.length -1:

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

So the last word is not processed.

Try with: longestWord("Pride AAAAAAAAAAAAAAAAAAAAAAAAA and Prejudice"). You'll see it works (returns AAAAAAAAAAAAAAAAAAAAAAAAA).

In case you're in doubt, the simplest way to fix it is removing the -1 from the for loop.

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

Check a demo with both versions (the problematic and the fixed): link here.

Find the Longest Word in a String in JavaScript - iDevOI, Finding the longest word in a string of text is a very common algorithmic challenge in the JavaScript world. In this article we examine three ways to solve this  If you want to return the length of the longest word, just add .length to the end of the return.


Finding the Longest Word In a Sentence ― Scotch.io, Your response should be a number. findLongestWordLength("The quick brown fox jumped over the lazy dog") should return  JavaScript: Find the longest string from an given array of strings. Last update on February 09 2019 08:35:39 (UTC/GMT +8 hours) Write a JavaScript to find the longest string from an given array of strings.


How to find the longest word in a String with JavaScript, JS Find the Longest Word in a String function findLongestWord(str) { var length; var stringArray = []; var string = str; stringArray = string.split('  i.e. input -> "Three ways to find the the longest word in a string in JavaScript" output -> "JavaScript" Based on the answers in the Medium article and comments on this post, what was meant was actually "Three ways to find the length of the longest word in a string in JavaScript". Also all answers are wrong, including those in the Medium article.


Find the Longest Word in a String with JavaScript, Find Longest Word in a String With JavaScript, Find the longest word in an array JavaScript, How to find the longest word in a string using JavaScript,Finding the Longest Word In a Sentence With JavaScript Hello Guys, i will tell you today through expertsphp tutorial that how will you fetch the words charecter with the …