กฎ Ulam (Collatz): n=1 หยุด, n คู่ → n/2, n คี่ → 3n+1 ทำซ้ำจนถึง 1ตัวอย่างโค้ด { const res = [num]; if (num && num === Math.abs(num) && isFinite(num)) { while (num !== 1) { if (num % 2) { num = 3 * num + 1; } else { num /= 2; } res.push(num); }
กฎ Ulam (Collatz): n=1 หยุด, n คู่ → n/2, n คี่ → 3n+1 ทำซ้ำจนถึง 1ตัวอย่างโค้ด { const res = [num]; if (num && num === Math.abs(num) && isFinite(num)) { while (num !== 1) { if (num % 2) { num = 3 * num + 1; } else { num /= 2; } res.push(num); }
ตัวเลข: 2.74 หาร 4 → 0.685 ปัด 2 ตำแหน่ง → 0.69 คำนวณเศษตัวอย่างโค้ด { const quo = +(num / parts).toFixed(precision); const remainder = +(num - quo * (parts - 1)).toFixed(precision); if (quo === remainder) { return { parts, value: quo }; } else { return { parts: parts - 1, value: quo
อ็อบเจ็กต์: {a: 4, b: 2, c: 5, d: 1, e: 3}หาค่า value ที่น้อยที่สุดตัวอย่างโค้ด { return Math.min(acc, obj[val]); }, Infinity); return smallest; }; console.log(findSmallestValue(obj));ผลลัพธ์1คำอธิบาย: Object.keys(obj) ได้ key ทั้งหมด reduce เริ่มต้น Infinity เปรียบเทียบ Math.min(acc, obj[va
อ็อบเจ็กต์ key เป็นตัวเลขสตริง: {0: Rakesh, 1: Dinesh, 2: Mohit, 3: Rajan, 4: Ashish}แปลงเป็นอาร์เรย์ตามลำดับ keyตัวอย่างโค้ด { res[+el] = obj[el]; }); return res; }; console.log(objectToArray(obj));ผลลัพธ์[ Rakesh, Dinesh, Mohit, Rajan, Ashish ]คำอธิบาย: Object.keys() ได้ key เรียงตามลำดับ
ฟังก์ชันรับ rows, cols, initial value คืน 2D arrayตัวอย่างโค้ด { return value; }); }); return arr; }; console.log(fillArray(cols, rows, val));ผลลัพธ์[ [ Example, Example, Example, Example, Example ], [ Example, Example, Example, Example, Example ], [ Example, Example, Example, Ex
หา LCM ของอาร์เรย์ตัวเลข ใช้ GCD (Greatest Common Divisor) ช่วยคำนวณสูตร: LCM(a, b) = a * b / GCD(a, b)ตัวอย่างโค้ด { return a * b / gcd2(a, b); }; let n = 1; for (let i = 0; i < arr.length; ++i) { n = lcm2(arr[i], n); } return n; }; console.log(calculateLCM(12, 18, 7, 15, 20, 24,
ใช้ toLocaleString(en-IN, {style: currency, currency: INR})ตัวอย่างโค้ด { const curr = num.toLocaleString(en-IN, { style: currency, currency: INR }); return curr; }; console.log(toIndianCurrency(num1)); console.log(toIndianCurrency(num2)); console.log(toIndianCurrency(num3));ผลลัพธ์₹1,
อาร์เรย์อ็อบเจ็กต์มี property date: [{id: 1, date: Mar 12 2012 10:00:00 AM}, {id: 2, date: Mar 8 2012 08:00:00 AM}]แปลงเป็น Date object เปรียบเทียบ timestamp เรียงลำดับตัวอย่างโค้ด { return new Date(a.date).getTime() - new Date(b.date).getTime(); }; arr.sort(sorter); }; sortByDate(arr); cons
อาร์เรย์: [{TR-01:1}, {TR-02:3}, {TR-01:3}, {TR-02:5}]รวมค่าของ key เดียวกัน: [{TR-01:4}, {TR-02:8}]ตัวอย่างโค้ด { const map = {}; for (let i = 0; i < arr.length; ) { const key = Object.keys(arr[i])[0]; if (!map.hasOwnProperty(key)) { map[key] = i++; continue; } arr[m
อาร์เรย์: [1,2,3,4,5,4,12,2,6,7,4,3,2]แปลงทุกค่าเป็นตัวเลขด้วย parseInt() คำนวณค่าเฉลี่ยตัวอย่างโค้ด { let sum = 0, count = 0; for (let i = 0; i < arr.length; i++) { const val = parseInt(arr[i]); if (val) { sum += val; count++; } } return (sum / count); }; console.log
อาร์เรย์: [a, b, c, d] ขนาดกลุ่ม: 2 → [[a,b], [c,d]]ตัวอย่างโค้ด { const res = []; for (let i = 0; i < arr.length; i++) { if (i % size === 0) { res.push([arr[i]]); } else { res[res.length - 1].push(arr[i]); } } return res; }; console.log(chunk(arr, n));ผลลัพธ์[ [ a, b
ช่วงหลัก R1: [20, 40] ช่วงย่อย R2: [[14,22], [24,27], [31,35], [38,56]]คำนวณความยาวส่วนที่ทับซ้อนของแต่ละช่วงใน R2 กับ R1 แล้วรวมกันตัวอย่างโค้ด { const [left, right] = val; const ex1 = Math.min(right, end); const ex2 = Math.max(left, start); const diff = ex1 - ex2; return acc +
อาร์เรย์: [Amy, Dolly, Jason, Madison, Patricia]กรองเฉพาะที่ตัวอักษรแรกอยู่ในช่วง a-j (รวม)ตัวอย่างโค้ด c1 { const [firstChar] = el.toLowerCase(); return isGreater(firstChar, start) && isSmaller(firstChar, end); }); return filtered; }; console.log(filterByAlphaRange(arr, a, j));ผลลัพธ์[
อาร์เรย์: [Amy, Dolly, Jason, Madison, Patricia]นับสระ (a, e, i, o, u) ทั้งหมดในทุกสตริงตัวอย่างโค้ด { for (let i = 0; i < el.length; i++) { if (isVowel(el[i])) { count++; } } }); return count; }; console.log(countVowels(arr));ผลลัพธ์10คำอธิบาย: legend เก็บสระ isVowel
อาร์เรย์: [{customer: Customer 1, project: 1}, {customer: Customer 2, project: 2}, {customer: Customer 2, project: 3}]จัดกลุ่ม customer เดียวกัน รวม project เป็นอาร์เรย์ตัวอย่างโค้ด { return el.customer === custom.customer; })[0]; if (customer) { customer.projects.push(el.project
แปลง key string object[foo][bar][ya] เป็น nested objectตัวอย่างโค้ด { if (Object.keys(res).length === 0) { res[key] = obj[Object.keys(obj)[0]]; } else { const temp = {}; temp[key] = res; res = temp; } }); return res; }; console.log(JSON.stringify(constructDotN
อ็อบเจ็กต์ 2 ตัว: {a: true, b: false, c: foo} และ {a: false, b: false, c: foo}หา key ที่มีในทั้งคู่ และ value เหมือนกันตัวอย่างโค้ด { if (obj1[key] === obj2[key] && obj2.hasOwnProperty(key)) { return true; } return false; }); return common; }; console.log(compareObjects(obj1, o
อาร์เรย์ตัวกรอง: [1, 2, 3, 4, 5]อาร์เรย์อ็อบเจ็กต์: [{city: 4}, {city: 6}, {city: 8}]คืนเฉพาะอ็อบเจ็กต์ที่ city อยู่ในอาร์เรย์ตัวกรองตัวอย่างโค้ด { return literalArr.includes(el[city]); }); return common; }; console.log(filterByLiterals(cities, source));ผลลัพธ์[ { city: 4 } ]คำอธิบาย: filter
อาร์เรย์: [1, 4, 2, 3, 0, 5] target sum: 7หาคู่ที่รวมได้ 7 ไม่ซ้ำคู่ (unique pairs) รวมดัชนีของทุกคู่คู่: 4+3 (index 1,3), 5+2 (index 5,2) → ผลรวมดัชนี = 1+3+5+2 = 11ตัวอย่างโค้ด { let copy = arr.slice(0); const used = []; let index = 0, indexFirst = 0, indexSecond, first, second; while (ind
มี JSON array ต้องจัดกลุ่มตาม key W แล้วเก็บ A1ตัวอย่างโค้ด { const temp = {}; temp[val] = preCombined[val]; acc.push(temp); return acc; }, []); return combined; }; console.log(JSON.stringify(groupJSON(arr), undefined, 4));ผลลัพธ์[ { 1: [ { A1: 123 }, { A1: 456