Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements