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

    หาเอลีเมนต์ที่ปรากฏในทั้งสองอาร์เรย์ (Intersection) นับความถี่ตามที่น้อยกว่า (Multiset Intersection) ตัวอย่าง arr1 = [hello, world, how, are, you] arr2 = [hey, world, can, you, rotate] Intersection: [world, you] ตัวอย่างโค้ด { const res = []; const smaller = arr1.length = arr2.length ? arr1 :

  2. การหาผลรวม Unique Elements ในอาร์เรย์ (Sum Duplicates Together) ใน JavaScript

    ภาพรวม บทความนี้สาธิตวิธีรวมค่าที่ซ้ำกันในอาร์เรย์ โดยบวกค่า Duplicate เข้าด้วยกัน กลายเป็น Unique Values พร้อม Sum ตัวอย่าง Input: [1, 3, 1, 3, 5, 7, 5, 4] Process: 1+1=2, 3+3=6, 5+5=10, 7, 4 Output: [2, 6, 10, 7, 4] (ลำดับตาม First Occurrence) การนำไปใช้ value); }; console.log(mergeDuplicate

  3. การหา Element กลางของอาร์เรย์โดยไม่ใช้ Length Property ใน JavaScript

    ภาพรวม บทความนี้สาธิตวิธีหา Element กลางของอาร์เรย์โดยไม่เข้าถึง .length และไม่ใช้ Loop แบบ Built-in (for/while) ใช้ Recursive แทน การนำไปใช้ { if (arr[ind]) { return midElement(arr, ++ind); } // ind ตอนนี้คือ Length return ind % 2 !== 0 ? [arr[(ind - 1) / 2]] : [arr[(ind / 2

  4. การนับตัวเลขต่ำกว่า/สูงกว่าค่าที่กำหนดในอาร์เรย์ JavaScript

    ภาพรวม บทความนี้สาธิตวิธีนับจำนวน Element ในอาร์เรย์ที่น้อยกว่า (Below) และมากกว่าหรือเท่ากับ (Par/Above) ค่าที่กำหนด ตัวอย่าง Array: [54,54,65,73,43,78,54,54,76,3,23,78] Threshold 50: Below=3 (43,3,23), Par=9 Threshold 60: Below=7, Par=5 Threshold 70: Below=8, Par=4 การนำไปใช้ { const isBel

  5. การหาความต่างของอาร์เรย์สองชุด (Symmetric Difference) ใน JavaScript

    ภาพรวม บทความนี้สาธิตวิธีหา Element ที่อยู่ในอาร์เรย์ใดอาร์เรย์หนึ่ง แต่ไม่อยู่ในทั้งสอง (Symmetric Difference) ตัวอย่าง arr1 = [12, 54, 2, 4, 6, 34, 3] arr2 = [54, 2, 5, 12, 4, 1, 3, 34] Difference: [6, 5, 1] (6 อยู่แค่ arr1, 5,1 อยู่แค่ arr2) การนำไปใช้ { const res = []; for (let i = 0; i

  6. การคำนวณ AND Product ของอาร์เรย์ Boolean 2 มิติใน JavaScript

    ภาพรวม บทความนี้สาธิตวิธีรวมอาร์เรย์ 2 มิติของ Boolean ด้วย AND Operator (&&) ตามคอลัมน์ ตัวอย่าง Input: [[true, false, false], [false, false, false], [false, false, true]] Column 0: true && false && false = false Column 1: false && false && false = false Column 2: false && false && true = false O

  7. การหาจำนวนเฉพาะทั้งหมดจนถึง n (Prime Sieve) ใน JavaScript

    ภาพรวม บทความนี้สาธิตวิธีหาจำนวนเฉพาะทั้งหมดตั้งแต่ 2 ถึง n โดยใช้ Trial Division (ไม่ใช่ Sieve of Eratosthenes) ตัวอย่าง Input: 24 Output: [2, 3, 5, 7, 11, 13, 17, 19, 23] การนำไปใช้ (Trial Division) { if (num < 2) return false; let count = 2; while (count { if (num < 2) return [];

  8. การแทนที่อักขระพิเศษด้วย ASCII Code ใน JavaScript

    ภาพรวม บทความนี้สาธิตวิธีแทนที่อักขระพิเศษ (Non-alphanumeric) ในสตริงด้วย ASCII Code ของมัน ตัวอย่าง 64 การนำไปใช้ { let res = ; for (let i = 0; i ASCII Code res += char.charCodeAt(0); } return res; }; console.log(specialToASCII(Th!s !s @ str!ng th@t cont@!ns some special characte

  9. หาผลคูณหลักของตัวเลข - JavaScript

    หาผลคูณของทุกหลักในตัวเลข หากมีหลักเป็น 0 ให้ถือเป็น 1 (ไม่ให้ผลคูณเป็น 0) ตัวอย่าง 5720 → 5 × 7 × 2 × 1(แทน 0) = 70 ตัวอย่างโค้ด (Recursive) { if (num) { return recursiveProduct(Math.floor(num / 10), res * (num % 10 || 1)); } return res; }; console.log(recursiveProduct(num)); ผลลัพธ์

  10. การลบสระตัวสุดท้ายของแต่ละคำใน JavaScript

    ภาพรวม บทความนี้สาธิตวิธีลบสระ (Vowel: a, e, i, o, u) ตัวสุดท้ายของแต่ละคำในประโยค ตัวอย่าง strng (i) การนำไปใช้ { return str.split( ) .map(removeLast) .join( ); }; console.log(removeLastVowel(This is an example string)); ผลลัพธ์ Ths s n exampl strng วิธีการทันสมัยและครอบคลุมมากก

  11. การตรวจสอบอาร์เรย์สตริง Progressive (เพิ่มตัวอักษรทีละตัว) ใน JavaScript

    ภาพรวม บทความนี้สาธิตวิธีตรวจสอบว่าอาร์เรย์สตริงที่จัดเรียงตามความยาว มีคุณสมบัติ Progressive: สตริงถัดไปเกิดจากการเพิ่มตัวอักษร 1 ตัวที่ด้านหน้าหรือด้านหลังของสตริงก่อนหน้า ตัวอย่าง dacane (+d หน้า) การนำไปใช้ { for (let i = 0; i < arr.length - 1; i++) { const next = arr[i + 1]; cons

  12. การนับตัวอักษรที่ซ้ำในสตริง (Redundant Characters) ใน JavaScript

    ภาพรวม บทความนี้สาธิตวิธีนับจำนวนตัวอักษรที่ซ้ำ (Redundant Characters) ในสตริง นับเป็นจำนวนครั้งที่เกินครั้งแรก ตัวอย่าง 3 (a ซ้ำ 2 ครั้งเพิ่ม, c ซ้ำ 1 ครั้งเพิ่ม = 3) การนำไปใช้ { let count = 0; for (let i = 0; i ซ้ำ if (i !== str.lastIndexOf(str[i])) { count++; } } retu

  13. การจัดเรียงตัวเลขภายในตัวเลข (Sort Digits Within Numbers) ใน JavaScript

    ภาพรวม บทความนี้สาธิตวิธีจัดเรียงหลักตัวเลขภายในแต่ละตัวเลขในอาร์เรย์ ให้เรียงจากน้อยไปมาก ตัวอย่าง 78 การนำไปใช้ arr.map(ascendNumber); console.log(sortDigits([543, 65, 343, 75, 567, 878, 87])); ผลลัพธ์ [345, 56, 334, 57, 567, 788, 78] คืนค่าเป็น Number แทน String a - b).join() )); co

  14. การสร้างอาร์เรย์ Mapping ตัวอักษร Unique ในสตริง JavaScript

    ภาพรวม บทความนี้สาธิตวิธี Mapping ตัวอักษรในสตริงเป็นตัวเลข: ตัวอักษรใหม่ (Unique) ได้หมายเลขเพิ่มขึ้น, ตัวซ้ำได้หมายเลขเดิม ตัวอย่าง 2 การนำไปใช้ { const res = []; let curr = , count = -1; for (let i = 0; i < str.length; i++) { if (str[i] === curr) { res.push(count); } el

  15. การหาตัวอักษรที่ปรากฏบ่อยเป็นอันดับสองใน JavaScript

    ภาพรวม บทความนี้สาธิตวิธีหาตัวอักษรที่มีความถี่เป็นอันดับสองในสตริง ตัวอย่าง Input: Hello world, I have never seen such a beautiful weather in the world Output: e การนำไปใช้ { const map = {}; for (let i = 0; i b[1] - a[1]); return freqArr[1][0]; }; console.log(secondFrequent(Hello world,

  16. การรวมสตริงสองตัวสลับกันทีละสองตัวอักษร (Twice Join) ใน JavaScript

    ภาพรวม บทความนี้สาธิตวิธีรวมสตริงสองตัว โดยสลับกันทีละ 2 ตัวอักษรจากแต่ละสตริง ตัวอย่าง str1 = Hello world str2 = How are you btw Output: HeHollw o arwoe rlyodu btw // He + Ho + ll + ow + ... (สับสนมาก) Logic: เข้าสลับกันทีละ 2 chars จาก str1, str1, str2, str2, str1, str1, ... การนำไปใช้ { le

  17. การตรวจสอบตัวเลข Pandigital (ครบ 0-9) ใน JavaScript

    ภาพรวม Pandigital Number คือตัวเลขที่มีตัวเลข 0-9 ครบทุกตัวอย่างน้อย 1 ครั้ง ตัวอย่าง true (ครบ 0-9) การนำไปใช้ { let legend = [0,1,2,3,4,5,6,7,8,9]; for (let i = 0; i < numStr.length; i++) { const idx = legend.indexOf(numStr[i]); if (idx !== -1) { legend.splice(idx, 1); //

  18. การนับกลุ่มตัวเลข Non-negative ที่ติดกันในอาร์เรย์ JavaScript

    ภาพรวม =0) ที่ติดต่อกันในอาร์เรย์ ตัวอย่าง Input: [-1,-2,-1,0,-1,-2,-1,-2,-1,0,1,0] Groups: [0] at index 3, [0,1,0] at index 9-11 Output: 2 การนำไปใช้ = 0 และ (ค่าถัดไป = 0 && (arr[ind + 1] < 0 || ind + 1 === arr.length)) { acc++; } return acc; }, 0); }; console.log(positiveClus

Total 5941 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:73/298  20-คอมพิวเตอร์/Page Goto:1 67 68 69 70 71 72 73 74 75 76 77 78 79