Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript
JavaScript
  1. การตรวจสอบการแบ่งอาร์เรย์เท่ากัน (Equal Partition) - JavaScript

    ตรวจสอบว่าสามารถแบ่งอาร์เรย์เป็น 2 กลุ่มได้หรือไม่: 1 สมาชิก และอีกส่วน ที่ผลคูณของอีกส่วนเท่ากับสมาชิกตัวนั้น ตัวอย่าง Input: [1, 56, 2, 4, 7] 56 = 1 × 2 × 4 × 7 = 56 → true ตัวอย่างโค้ด max || !max) { prod *= (max || 1); // คูณค่าสูงสุดเดิม max = val; // อัปเดตค่าสูงสุดใหม่

  2. การคำนวณ Lunar Sum ของตัวเลข - JavaScript

    Lunar Sum (หรือ Dismal Addition) คือการบวกตัวเลขโดยไม่ได้บวกหลักตรงกัน แต่เอาค่าที่มากกว่าของแต่ละหลัก ตัวอย่าง 879 + 768 หลักร้อย: max(8,7) = 8 หลักสิบ: max(7,6) = 7 หลักหน่วย: max(9,8) = 9 ผลลัพธ์: 879 ตัวอย่างโค้ด { const numStr1 = String(num1); const numStr2 = String(num2); let res = 0

  3. การหาความยาวคำเฉลี่ยในประโยค - JavaScript

    คำนวณความยาวเฉลี่ยของคำในประโยค ปัดเศษ 2 ตำแหน่งทศนิยม ตัวอย่างโค้ด { if (!str.includes( )) return str.length; const { length: strLen } = str; const { length: numWords } = str.split( ); // strLen - (numWords - 1) = จำนวนตัวอักษรรวม (ลบช่องว่าง) const average = (strLen - numWords + 1) /

  4. การตรวจสอบ Full House ในโป๊กเกอร์ - JavaScript

    Full House ในโป๊กเกอร์ = มีบัตร 3 ใบเหมือนกัน (three of a kind) โค้ดนี้ตรวจสอบว่ามี full house หรือไม่ ตัวอย่างโค้ด { const copy = arr.slice(); for (let i = 0; i < arr.length; ) { const el = copy.splice(i, 1)[0]; // ดึงเอาตัวที่ i ออก if (copy.includes(el)) { copy.splice(copy.ind

  5. การแมปตัวอักษรในสตริงเป็นออบเจ็กต์ของตำแหน่ง - JavaScript

    สร้างออบเจ็กต์ที่ key คือตัวอักษร value คืออาร์เรย์ของตำแหน่ง (index) ที่ตัวอักษรนั้นปรากฏ ตัวอย่าง // Input: cannot // Output: { c: [0], a: [1], n: [2, 3], o: [4], t: [5] } ตัวอย่างโค้ด { const map = {}; for (let i = 0; i < str.length; i++) { if (map.hasOwnProperty(str[i])) { map[s

  6. การเพิ่มฟังก์ชัน swapCase เข้า String Prototype - JavaScript

    เพิ่มเมธอด swapCase() ให้ String.prototype เพื่อสลับตัวพิมพ์ใหญ่/เล็กของทุกตัวอักษร ตัวอย่างโค้ด const str = ThIS iS A CraZY StRInG; String.prototype.swapCase = function() { let res = ; for (let i = 0; i < this.length; i++) { // ไม่ใช่ตัวอักษร (ตัวเลข, สัญลักษณ์, ช่องว่าง) if (this[i].

  7. การหาตัวอักษรที่ซ้ำกันระหว่างสองสตริง - JavaScript

    หาตัวอักษรที่ปรากฏในทั้งสองสตริง (common characters) คืนเป็นสตริง (รวมซ้ำ) ตัวอย่าง str1 = Hey There!!, how are you str2 = Can this be a special string // Output: e here h are (ตัวอักษรที่ซ้ำกัน ตามลำดับใน str1) ตัวอย่างโค้ด { let res = ; for (let i = 0; i < str1.length; i++) { if (!str2.

  8. การหาระยะห่างระหว่างตัวอักษรสองตัวในสตริง - JavaScript

    หาระยะห่าง (index difference) ระหว่างตัวอักษรสองตัวในสตริง ตัวอย่าง str = Disaster management a = i, b = t // i อยู่ index 4, t อยู่ index 8 → ระยะห่าง = 4 ตัวอย่างโค้ด { const aIndex = str.indexOf(a); const bIndex = str.indexOf(b); if (aIndex === -1 || bIndex === -1) return false; return

  9. การจัดเรียงอาร์เรย์ตามลำดับมาตรฐาน - JavaScript

    จัดเรียงอาร์เรย์ dynamic ตามลำดับที่กำหนดในอาร์เรย์มาตรฐาน (reference array) ตัวอย่าง dbArray = [Apple,Banana,Mango,Apple,Mango,Mango,Apple] stdArray = [Mango,Apple,Banana,Grapes] result = [Mango,Mango,Mango,Apple,Apple,Apple,Banana] ตัวอย่างโค้ด ref.indexOf(a) - ref.indexOf(b); arr.so

  10. ทำไม ([] == false) เป็น true แต่ ([] || true) กลับเป็น [] ? - JavaScript

    บทความนี้อธิบายพฤติกรรมที่แตกต่างของ JavaScript ระหว่าง loose equality (==) และ logical OR (||) กรณี 1: ([] == false) → true ขั้นตอนการแปลงแบบ (Type Coercion): false แปลงเป็น Number → 0 [] (Object) แปลงเป็น Primitive → (empty string) แปลงเป็น Number → 0 0 == 0 → true กรณี 2: ([] || tru

  11. การตรวจสอบ Pandigital Number - JavaScript

    Pandigital Number คือตัวเลขที่มีตัวเลข 0-9 ครบทุกตัวอย่างน้อย 1 ครั้ง ตัวอย่าง 47458892414 → false (ขาด 0, 3, 6) 53657687691428890 → true (มี 0-9 ครบ) ตัวอย่างโค้ด { let legend = [0,1,2,3,4,5,6,7,8,9]; for (let i = 0; i < numStr.length; i++) { if (!legend.includes(numStr[i])) contin

  12. โปรแกรมแปลงอุณหภูมิ - JavaScript

    แปลงอุณหภูมิระหว่าง Celsius และ Fahrenheit จากสตริงที่ลงท้ายด้วย C หรือ F สูตร C → F: (C × 9/5) + 32 F → C: (F - 32) × 5/9 ตัวอย่างโค้ด { const degree = temp[temp.length - 1]; let converted; if (degree === C) { converted = (parseInt(temp) * 9 / 5 + 32).toFixed(2) + F; } else {

  13. การกลับลำดับคำที่มีความยาวเป็นจำนวนเฉพาะ - JavaScript

    กลับลำดับตัวอักษรในคำที่มีความยาวเป็นจำนวนเฉพาะ (prime length: 2, 3, 5, 7, 11, ...) ตัวอย่าง // Input: His father is an engineer by profession // ความยาวคำ: His(3) father(6) is(2) an(2) engineer(8) by(2) profession(10) // Prime lengths: 3, 2, 2, 2 → His, is, an, by // Output: siH father si na engin

  14. การแปลงเศษส่วนเหมาะสมเป็นเศษส่วนผสม - JavaScript

    แปลงเศษส่วน p/q (proper fraction) เป็นเศษส่วนผสม (mixed fraction) q r/b โดย q = ผลหาร, r = เศษ, b = ตัวหาร ตัวอย่าง // Input: [43, 13] (43/13) // 43 ÷ 13 = 3 เศษ 4 // Output: [3, 4, 13] → 3 wholes and 4/13 ตัวอย่างโค้ด { const quotient = Math.floor(arr[0] / arr[1]); const remainder = arr[0] %

  15. การจัดเรียงแบบ Priority Sort - JavaScript

    จัดเรียงอาร์เรย์แรกให้เรียงน้อยไปมาก แต่ย้ายสมาชิกที่มีในอาร์เรย์ที่สอง (priority array) ไปอยู่ข้างหน้าสุด ตัวอย่าง arr1 = [5, 4, 3, 2, 1] arr2 = [2, 3] // priority elements Output: [2, 3, 1, 4, 5] ตัวอย่างโค้ด sorter(a, b, arr2)); }; prioritySort(arr1, arr2); console.log(arr1); การทำงานของ Co

  16. การนับข้อผิดพลาดในสตริงที่พิมพ์ผิด - JavaScript

    เปรียบเทียบสตริงที่พิมพ์ผิด (mistyped) กับสตริงที่ถูกต้อง (correct) นับจำนวนตำแหน่งที่ตัวอักษรต่างกัน ตัวอย่าง mistaken: Tkos am er exakgrg fetwnh correct: This is an example string // ต่างกัน 15 ตำแหน่ง ตัวอย่างโค้ด { let count = 0; for (let i = 0; i < mistaken.length; i++) { if (mistak

  17. การหาการกระจายความถี่ของตัวอักษรในสตริง - JavaScript

    ฟังก์ชันนี้คืนออบเจ็กต์ที่แสดงความถี่ของแต่ละตัวอักษรในสตริง (รวมช่องว่างและสัญลักษณ์) ตัวอย่างโค้ด { const map = {}; for (let i = 0; i < str.length; i++) { map[str[i]] = (map[str[i]] || 0) + 1; } return map; }; console.log(frequencyDistribution(str)); วิธีทันสมัยกว่า (acc[ch] = (ac

  18. การสร้าง Promise ที่ resolve/reject ตามเงื่อนไข - JavaScript

    ฟังก์ชันนี้จำลอง network request ด้วย setTimeout และคืน Promise ที่ resolve เมื่อคำนวณถูกต้อง reject เมื่อผิด ตัวอย่างโค้ด console.log(err)); การทำงาน สร้าง Promise ใหม่ ใช้ setTimeout จำลอง async operation (3 วินาที) ตรวจสอบเงื่อนไข resolve/reject ใช้ .then()/.catch() จัดการผลลัพธ์ ผล

  19. การกรองจำนวนเฉพาะออกจากอาร์เรย์ - JavaScript

    ฟังก์ชันนี้กรองอาร์เรย์ให้เหลือเฉพาะตัวเลขที่ไม่ใช่จำนวนเฉพาะ (non-prime) ตัวอย่าง Input: [34, 56, 3, 56, 4, 343, 68, 56, 34, 87, 8, 45, 34] Output: [34, 56, 56, 4, 343, 68, 56, 34, 87, 8, 45, 34] (ตัด 3 ออก) ตัวอย่างโค้ด { if (n === 1) return false; if (n === 2) return true; for (let x = 2

  20. การหาสมาชิกที่ไม่ซ้ำในอาร์เรย์ - JavaScript

    คืนอาร์เรย์ของสมาชิกที่ปรากฏเพียงครั้งเดียว (unique/distinct elements) ตัวอย่าง // Input: [9, 5, 6, 8, 7, 7, 1, 1, 1, 1, 1, 9, 8] // Output: [5, 6] (เฉพาะ 5 และ 6 ที่ปรากฏครั้งเดียว) ตัวอย่างโค้ด { const res = []; for (let i = 0; i < arr.length; i++) { // indexOf === lastIndexOf → ปรากฏคร

Total 5941 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:92/298  20-คอมพิวเตอร์/Page Goto:1 86 87 88 89 90 91 92 93 94 95 96 97 98