เราต้องเขียนโปรแกรม JavaScript ที่กลับสตริงโดยใช้ตัวแปรเพิ่มเติมเพียงตัวเดียว ไม่ใช้ฟังก์ชันในตัวหรือฟังก์ชันที่สร้างเอง ใช้แค่ลูปพื้นฐาน
ตัวอย่างโค้ด
const string = 'abcdef';
let reverse = '';
while (reverse.length !== string.length) {
const index = string.length - 1 - reverse.length;
reverse += string[index];
}
console.log(reverse);
ผลลัพธ์
ค่าที่แสดงในคอนโซลจะเป็นดังนี้:
fedcba