Checking smooth sentences in JavaScript


We are required to write a JavaScript function that checks whether a sentence is smooth or not. A sentence is smooth when the first letter of each word in the sentence is same as the last letter of its preceding word.

Example

Following is the code −

const str = 'this stringt tries sto obe esmooth';
const str2 = 'this string is not smooth';
const isSmooth = str => {
   const strArr = str.split(' ');
   for(let i = 0; i < strArr.length; i++){
      if(!strArr[i+1] || strArr[i][strArr[i].length -1] === strArr[i+1]
      [0]){
         continue;
      };
      return false;
   };
   return true;
};
console.log(isSmooth(str));
console.log(isSmooth(str2))

Output

Following is the output in the console −

true
false

Updated on: 16-Sep-2020

161 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements