Smooth Sentence คือประโยคที่ตัวอักษรสุดท้ายของแต่ละคำ ตรงกับตัวอักษรแรกของคำถัดไป
ตัวอย่าง
'this stringt tries sto obe esmooth' // true 'this string is not smooth' // false
ตัวอย่างโค้ด
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)); // true
console.log(isSmooth(str2)); // false
การทำงาน
- แยกคำด้วย
split(' ') - เปรียบเทียบ
strArr[i][last]กับstrArr[i+1][0] - ข้ามคำสุดท้าย (ไม่มีคำถัดไป)
ผลลัพธ์
true false
ประยุกต์ใช้: เกมคำ (word chain) การตรวจสอบภาษาศาสตร์ การสร้างปริศนา