Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Find the Longest Word in a String

// Find the Longest Word in a String

function findLongestWordLength(str) {
	let longest = '';
	let words = str.split(' ');
	for (let i = 0; i < words.length; i++) {
		if (longest.length < words[i].length) longest = words[i];
	}
	return longest.length;
}

findLongestWordLength('The quick brown fox jumped over the lazy dog');

// OR

function findLongestWordLength(s) {
	return s.split(' ').reduce(function (longest, word) {
		return Math.max(longest, word.length);
	}, 0);
}

// OR

// My favourite
function findLongestWordLength(str) {
	return Math.max(...str.split(' ').map((word) => word.length));
}

// OR

function findLongestWordLength(str) {
	// split the string into individual words
	const words = str.split(' ');

	// words only has 1 element left that is the longest element
	if (words.length == 1) {
		return words[0].length;
	}

	// if words has multiple elements, remove the first element
	// and recursively call the function
	return Math.max(words[0].length, findLongestWordLength(words.slice(1).join(' ')));
Comment

Find the Longest Word in a String

// Find the Longest Word in a String

function findLongestWordLength(str) {
	let longest = '';
	let words = str.split(' ');
	for (let i = 0; i < words.length; i++) {
		if (longest.length < words[i].length) longest = words[i];
	}
	return longest.length;
}

findLongestWordLength('The quick brown fox jumped over the lazy dog');

// OR

function findLongestWordLength(s) {
	return s.split(' ').reduce(function (longest, word) {
		return Math.max(longest, word.length);
	}, 0);
}

// OR

// My favourite
function findLongestWordLength(str) {
	return Math.max(...str.split(' ').map((word) => word.length));
}

// OR

function findLongestWordLength(str) {
	// split the string into individual words
	const words = str.split(' ');

	// words only has 1 element left that is the longest element
	if (words.length == 1) {
		return words[0].length;
	}

	// if words has multiple elements, remove the first element
	// and recursively call the function
	return Math.max(words[0].length, findLongestWordLength(words.slice(1).join(' ')));
}
Comment

longest word in a string

function findLongestWordLength(str) {
  return Math.max(...str.split(" ").map(word => word.length));
}
Comment

Finding the longest word in a string

// https://dev.to/estheragbaje/three-ways-to-find-the-longest-word-in-a-string-using-javascript-5236

// For Loop
function findLongestWord(str) {
  const splStrArray = str.split(' ');

  //initialize a variable to store the longest word
  let longestWord = "";
  for(let index = 0; index < splStrArray.length; index++){
    if(splStrArray[index].length > longestWord.length){
         longestWord = splStrArray[index];
     }
  }
 return longestWord // put.length for integer result
}
Comment

longest word in str

def longestWord(some_list): 
    count = 0    #You set the count to 0
    for i in some_list: # Go through the whole list
        if len(i) > count: #Checking for the longest word(string)
            count = len(i)
            word = i
    return ("the longest string is " + word)
Comment

Find the Longest Word in a String

function findLongestWordLength(str) {

  let arr = str.split(' '); 
  let maxLength = 0;

  for (let i = 0; i < arr.length; i++){
    if (arr[i].length >= maxLength){
      maxLength = arr[i].length;
    }
  } 

  return maxLength;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js set cursor final input 
Javascript :: appolo query data in angular graphql 
Javascript :: how to create an object that stores personal data in javascript 
Javascript :: function directory javascript 
Javascript :: javascript const scope = await angular.element(document.body).scope(); 
Javascript :: should i use map for form fields react 
Javascript :: error 28 mongodb 
Javascript :: why typescript is superset of javascript 
Javascript :: save specific attributes in table: sequelize 
Javascript :: resource loads fastest 
Javascript :: route methods 
Javascript :: javascript intersection recursion 
Javascript :: desctructuring in react with aliases 
Javascript :: automatically adjust color 
Javascript :: response conditions 
Javascript :: csvString to json 
Javascript :: if conprimido js 
Javascript :: Get client or user ip address in react using axios 
Javascript :: Enable Cookies and JavaScript in Internet Explorer 9.0 
Javascript :: pass js variable to css animation 
Javascript :: generic product filter javascript 
Javascript :: react native helper packages 
Javascript :: loop data from data base laravel to javascript 
Javascript :: force dom render 
Javascript :: {"javascript error: Invalid or unexpected token c# selenium 
Javascript :: Pass File and JSON Data Both at Same time in Postman API 
Javascript :: color blur in echart 
Javascript :: lodash uniqBy alterantive in js 
Javascript :: async loop with mongoose 
Javascript :: rails + vue js projcet demo 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =