Javascript Challenges - Longest Words

preview_player
Показать описание
Javascript Challenges - Longest Words

COUPONS BELOW!!!!!!

REACT

JAVASCRIPT COURSE

BOOTSTRAP COURSE

IN DEPTH HTML AND CSS

RESPONSIVE WEBISTES

JQUERY COURSE

FLEXBOX COURSE

GRID COURSE

RESPONSIVE COFFEE SHOW WEBSITE

RESPONSIVE CAR DEALERSHIP

COUPONS BELOW!!!!!!

JAVASCRIPT COURSE

BOOTSTRAP COURSE

IN DEPTH HTML AND CSS

RESPONSIVE WEBISTES

JQUERY COURSE

FLEXBOX COURSE

GRID COURSE

Products I Use:

Books I Recommend:

Disclosure: This video is not sponsored. Some links above are affiliate links, and l may earn a small commission from any purchases at no additional cost to you. Thank you for supporting my channel!
Рекомендации по теме
Комментарии
Автор

Using Map and filter



const longestWords = (str) => {
const maxLength = Math.max(...str.split(' ').map((el) => el.length));
return str.split(' ').filter((el) => el.length === maxLength);
};

bishalshah
Автор

my solution when i tried the challenge

function longestWords(str) {
let words = str.split(' ');
let arrayLengths = words.map(e => e.length);
let maxLength = Math.max(...arrayLengths);
let longest = words.filter(e => e.length === maxLength);
return longest;
}

console.log(longestWords('i woke up early today'));
console.log(longestWords('i went straight to the beach'));

GodsDevilthF
Автор

Love these series. I've followed a similar path and got the size of the longest word, but shortened up a little but when filtering and pushing it into the array:

function longestWords(str) {
let words = str.split(" ")
let size = 0;
let max = [""]
words.forEach((word) => {
if(word.length >= size)size = word.length})
max = words.filter(word => word.length === size)
return max
}

basically, after determining the longest size, i just pushed all the words whose length is equal to the size in to the array of max.

lichesschess
Автор

function longestWords(str) {
let theLongest = [""];

str.split(" ").forEach((element) => {
if (element.length > theLongest[0].length) {
theLongest = [];
theLongest.push(element);
} else if (element.length == theLongest[0].length) {
theLongest.push(element);
}
});
return theLongest;
}

leninsantiago
Автор

Thanks a lot! Great video! A suggestion to make a code more straightforward. Variable ‘size’ can be used as a length of previous max words, so there is no need to initialize max array with any value.
function longestWords(str) {
let words = str.split(' ');
let max = [];
let size = 0;

for (let i = 0; i < words.length; i++) {

let word = words[i];
if (word.length > size) {
size = word.length;
max = [word];
} else if (word.length === size) {
max = [...max, word];
}
}

return [...max];

}

bulatbaltin
Автор

const longestWords = (str) => {
const items = str.split(' ');
let longword = "";
items.map((item, index) => {
if(item.length > longword.length)
longword = item;
});
return items.filter(item => item.length === longword.length);
}

moulaoui
Автор

function longestWord(str) {
const words = str.split(' ');
const longest = Math.max(...words.map((word) => word.length));
return words.filter((word) => word.length === longest);
}

console.log(longestWord('I woke up early today')); // [ 'early', 'today' ]
console.log(longestWord('I went straight to the beach')); // [ 'straight' ]

noirsociety
Автор

Very nice man, simple and clean explanation. Hope you keep uploading content like this, helps a lot to keep understanding / learning javascript.


Thanks!

DEV_XO
Автор

You are awesome! Hope you will upload Javascript challenges more than one in a day. Thank you.

aboutthebook
Автор

This is awesome tutorials problematical

rasheds_miya
Автор

what does it take to know so much in JavaScript, How do I become good at coding pls. any guide ???

alexotoous
Автор

I think I did it with more simpler way. In your code, some points are unclear, as for instance why do we need size?

function longestWords(arr){
let x = arr.split(' ');
let longest = '';
let newArr = [];
for(let i=0; i<x.length; i++){
if(longest.length < x[i].length){
newArr = [];
longest = x[i];
newArr.push(longest)
} else if(longest.length === x[i].length){
longest = x[i]
newArr.push(longest)
}
}
return newArr;
}
my reason forthy doing the themes is to aware lorem help'))

B
Автор

I am new at Javascript but the words "early" and "today" in the first sentence are the same length. Doe's Javascipt only show the first word in alphabetical order and then stops?

cjwebdev
Автор

Can be and better !!!! I did not like so much ! its hard to understand, its seem to be easy to understand how at the end he is explaining very bad ! He likes to confuse us .

gheorgheslicari