ใช้ลูป for นับจากค่าเริ่มต้น ถึงค่าสิ้นสุด
ตัวอย่างโค้ด
var startValue = 10;
var endValue = 20;
var total = '';
function printAllValues(startValue, endValue) {
for (var start = startValue; start < endValue; start++) {
total = total + start + ",";
}
}
printAllValues(startValue, endValue);
var allSequences = total.slice(0, -1); // ตัด comma ตัวสุดท้าย
console.log(allSequences);
วิธีรันโปรแกรม
ใช้คำสั่ง node fileName.js (เช่น node demo87.js)
ผลลัพธ์
10,11,12,13,14,15,16,17,18,19