ปัญหา
เราต้องเขียนฟังก์ชัน JavaScript ที่รับสตริง str เข้ามา ฟังก์ชันควรสร้างสตริงใหม่ที่มีเฉพาะตัวอักษรที่ไม่ซ้ำกัน (unique characters) จากสตริงต้นฉบับ และลบทุกครั้งที่ตัวอักษรซ้ำกันออก
ตัวอย่าง
โค้ดตัวอย่างดังนี้:
const str = 'hey there i am using javascript';
const removeAllDuplicates = (str = '') => {
let res = '';
for(let i = 0; i < str.length; i++){
const el = str[i];
if(str.indexOf(el) === str.lastIndexOf(el)){
res += el;
continue;
};
};
return res;
};
console.log(removeAllDuplicates(str));
ผลลัพธ์
ผลลัพธ์ในคอนโซล:
Ymungjvcp