“Palavra mais longa em uma corda” Respostas de código

Encontre a palavra mais longa em uma corda

// 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(' ')));
YosKa

Encontre a palavra mais longa em uma corda

// 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(' ')));
}
YosKa

Palavra mais longa em uma corda

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

Respostas semelhantes a “Palavra mais longa em uma corda”

Perguntas semelhantes a “Palavra mais longa em uma corda”

Mais respostas relacionadas para “Palavra mais longa em uma corda” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código