Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript find shortest word in string

const findShort = str => {
  // Convert string into an array of individual words:
  const arr = str.split(' ');
  // Sort array in ascending order:
  arr.sort((a, b) => a.length - b.length);
  // Return the first (shortest) element in the array:
  return arr[0];
};
Comment

JavaScript find the shortest 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

PREVIOUS NEXT
Code Example
Javascript :: javascript on scroll change nav color 
Javascript :: get current location city name react native 
Javascript :: vue electron name and icon 
Javascript :: react bootsrap color picker 
Javascript :: image onclick react 
Javascript :: convert string to object javascript 
Javascript :: jquery element befor 
Javascript :: phaser2 align text center 
Javascript :: why does an array index start at 0 
Javascript :: node powershell 
Javascript :: limit data with axios in react js 
Javascript :: show a div in jquery 
Javascript :: conditional (ternary) operator function parameter 
Javascript :: disable URL encoder javascript 
Javascript :: javascript on enter key searchbox 
Javascript :: how to convert utc time to local time angular 
Javascript :: bfs javascript 
Javascript :: mongoose where 
Javascript :: react js bootstrap select option required 
Javascript :: url encoded body in node.js 
Javascript :: js date subtract minutes 
Javascript :: how to add element in json object 
Javascript :: add multiple elements to set javascript 
Javascript :: setImmediate() nodejs 
Javascript :: promise in javascript 
Javascript :: for in js 
Javascript :: filter array inside array of objects javascript 
Javascript :: Mongoose and multiple database in single node.js project 
Javascript :: jquery slideshow autoplay 
Javascript :: node.js brotli 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =