Finding special kind of sentences (smooth) in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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 the same as the last letter of its preceding word.

Therefore, let’s write the code for this function −

Example

The code for this will be −

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

The output in the console will be −

true
false
raja
Published on 17-Oct-2020 11:43:14
Advertisements