เราจำเป็นต้องเขียนฟังก์ชันที่ใช้สองสตริง เราต้องส่งคืนสตริงใหม่ซึ่งเหมือนกับอาร์กิวเมนต์แรกจากสองอาร์กิวเมนต์ แต่มีอาร์กิวเมนต์ที่สองอยู่ข้างหน้าทุกคำ
ตัวอย่างเช่น −
Input → ‘hello stranger, how are you’, ‘@@’ Output → ‘@@hello @@stranger, @@how @@are @@you’
หากไม่มีอาร์กิวเมนต์ที่สอง ให้ใช้ '#' เป็นค่าเริ่มต้น
ตัวอย่าง
const str = 'hello stranger, how are you';
const prependString = (str, text = '#') => {
return str
.split(" ")
.map(word => `${text}${word}`)
.join(" ");
};
console.log(prependString(str));
console.log(prependString(str, '43')); ผลลัพธ์
ผลลัพธ์ในคอนโซลจะเป็น -
#hello #stranger, #how #are #you 43hello 43stranger, 43how 43are 43you