how to reduce length of sentences in js code example

Example 1: javascript truncate string full word

const truncate = (str, max, suffix) => str.length < max ? str : `${str.substr(0, str.substr(0, max - suffix.length).lastIndexOf(' '))}${suffix}`;

// Example
truncate('This is a long message', 20, '...');

Example 2: How to get length of string in javascript without using native length method

var str = userInput[0];
  var count = 0;
  while(str[count] !== undefined)
  {
      count += 1;
  }
  console.log(count)

Tags:

Cpp Example