Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

longest word javascript

function solution(text) {
    return text.match(/[a-zA-Z]+/g).sort((a, b) => b.length - a.length)[0];
}
Comment

find longest word in string javascript

 function data(str){
           var show = str.split(" ");
            show.sort(function (a,b){
                return b.length - a.length; 
            })
            return show[0];
      }
      console.log(data(str = "javascript is my favourite language "));
Comment

js find longest word in string function

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

all longest strings javascript

function solution(inputArray) {
    return inputArray.reduce((r, s, i) => {
        if (!i || r[0].length < s.length) {
            return [s];
        }
        if (r[0].length === s.length) {
            r.push(s);
        }
        return r;
    }, []);
}
Comment

javascript find the longest word in a string

let str = 'Sleep is critical for good health. Not getting enough sleep can lead 
to diminished brain performance and, in the long term, greater risk of 
health conditions. These include heart disease, stroke, and diabetes';

function findWordsByLen(str, len) {
  const punctuationReg = /[.:;,?!"`~[]{}()<>/@#$%^&*=+_-]/g;
  const words = str
	.replaceAll(/s{2,}/g, ' ')
	.replaceAll(punctuationReg, '')
	.split(' ');

  let wordsByLen = words.reduce((acc, word) => {
    let wordLen = word.length;
    if (!acc.has(wordLen)) acc.set(wordLen, new Set());
    acc.get(wordLen).add(word);
    return acc;
  }, new Map());

  const maxLen = Math.max(...wordsByLen.keys());
  const minLen = Math.min(...wordsByLen.keys());

  switch(len) {
  case 'all':
    return wordsByLen;
  case 'max':
    return wordsByLen.get(maxLen);
  case 'min':
    return wordsByLen.get(minLen);
  }

  if (Number.isInteger(len) && wordsByLen.has(len)) return wordsByLen.get(len)
  else return `The string doesn't have any words with a length of ${len}`

}

findWordsByLen(str, 'max');     // => Set(1) { 'performance' }
findWordsByLen(str, 'min');     // => Set(4) { 'is', 'to', 'in', 'of' }
findWordsByLen(str, 8);         // => Set(2) { 'critical', 'diabetes' }


findWordsByLen(str, 'all');
/* OUTPUT:
Map(9) {
  5 => Set(5) { 'Sleep', 'sleep', 'brain', 'These', 'heart' },
  2 => Set(4) { 'is', 'to', 'in', 'of' },
  8 => Set(2) { 'critical', 'diabetes' },
  3 => Set(5) { 'for', 'Not', 'can', 'and', 'the' },
  4 => Set(5) { 'good', 'lead', 'long', 'term', 'risk' },
  6 => Set(3) { 'health', 'enough', 'stroke' },
  7 => Set(4) { 'getting', 'greater', 'include', 'disease' },
  10 => Set(2) { 'diminished', 'conditions' },
  11 => Set(1) { 'performance' }
}
*/
Comment

how to find the longest string in given string using js

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

JS longest word

const wordsArray = ['1', '22', '222', 'longstring', '2222']
const longestWord = wordsArray.reduce((longestWord, currentWord) =>
    currentWord.length > longestWord.length ? currentWord: longestWord, '');
console.debug(longestWord); // longstring
Comment

find longest word in a string javascript

function longestWord(string){
  let words = string.split(' ');
  let lengthArray = [];
  let maxNumber;
  let largestWord = '';
  
  for(let i = 0; i < words.length; i++){
    lengthArray.push(words[i].length);
  }
  
  maxNumber = Math.max(...lengthArray);
  
  largestWord = words[lengthArray.indexOf(maxNumber)];
  
  return largestWord;
}

let str = "The quick brown fox jumped over the lazy dogs";
let longWord = longestWord(str);

console.log(longWord); //output is jumped
Comment

PREVIOUS NEXT
Code Example
Javascript :: var y=5 
Javascript :: range between two numbers 
Javascript :: javascript closest child 
Javascript :: join string js with and at the last item 
Javascript :: ex: javascript loop array 
Javascript :: javascript round big numbers 
Javascript :: JSON to Ruby Hash Parser 
Javascript :: circle collision javascript 
Javascript :: js multi section listbox 
Javascript :: what i sminify javascript 
Javascript :: js no new line console.log 
Python :: pygame disable message 
Python :: tkinter make window not resizable 
Python :: python today - 1 day 
Python :: selenium Keys enter python 
Python :: python get current file location 
Python :: merge on index pandas 
Python :: pandas read csv no index 
Python :: python print time 
Python :: download files from google colab 
Python :: remove python ubuntu 
Python :: get statistics from list python 
Python :: colab save figure 
Python :: ImportError: dynamic module does not define module export function (PyInit_cv_bridge_boost) 
Python :: get current site django 
Python :: django return httpresponse 
Python :: python repeat every n seconds 
Python :: how to take array input in python in single line 
Python :: python reload module without restarting 
Python :: django import response 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =