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

    ลบช่องว่าง (whitespace) ทั้งหมดออกจากสตริง คืนค่าสตริงต่อเนื่อง ตัวอย่างโค้ด { let newStr = ; for (let i = 0; i < str.length; i++) { if (str[i] !== ) newStr += str[i]; } return newStr; }; console.log(removeWhitespaces(str)); ผลลัพธ์ Thisisanexamplestringfromwhichallwhitespaceswillbe

  2. หาตัวประกอบเฉพาะทั้งหมดของตัวเลข - JavaScript

    หาตัวประกอบเฉพาะ (Prime Factors) ของตัวเลข คือจำนวนเฉพาะที่หารตัวเลขนั้นลงตัว ตัวอย่าง 18 = 2 × 3 × 3 → Prime Factors: [2, 3] ตัวอย่างโค้ด { if (n < 2) return false; for (let i = 2; i { const res = num % 2 === 0 ? [2] : []; let factor = 3; while (factor <= num) { if (num % factor =

  3. สลับตัวพิมพ์เล็ก-ใหญ่ในสตริง (Flip Case) - JavaScript

    แปลงตัวพิมพ์ใหญ่เป็นเล็ก และตัวพิมพ์เล็กเป็นใหญ่ ในสตริง โดยใช้ ASCII Code ตัวอย่าง The Case OF tHis StrinG Will Be FLiPped → tHE cASE of ThIS sTRINg wILL bE flIpPED ตัวอย่างโค้ด = 65 && char.charCodeAt(0) = 97 && char.charCodeAt(0) { let newStr = ; const margin = 32; // ASCII difference betw

  4. หาคำที่ซ้ำในสตริง - JavaScript

    หาคำที่ปรากฏมากกว่า 1 ครั้งในสตริง คืนค่าเป็นสตริงรวมคำซ้ำ (ไม่ซ้ำกัน) ตัวอย่าง big black bug bit a big black dog on his big black nose → big black ตัวอย่างโค้ด const str = big black bug bit a big black dog on his big black nose; const findDuplicateWords = str => { const words = str.split( )

  5. หารากของสมการกำลังสอง (Quadratic Equation) - JavaScript

    หาค่าราก (Roots) ของสมการกำลังสอง ax² + bx + c = 0 โดยใช้สูตรกำลังสอง สูตรกำลังสอง x = (-b ± √(b² - 4ac)) / 2a ตัวอย่างโค้ด const coefficients = [3, 12, 2]; // 3x² + 12x + 2 = 0 const findRoots = co => { const [a, b, c] = co; const discriminant = b * b - 4 * a * c; if (discriminant <

  6. หาเลขยกกำลัง 2 ใกล้เคียงที่สุด - JavaScript

    หาเลขที่เป็นกำลังของ 2 (2ⁿ) ที่ใกล้ที่สุดกับตัวเลขที่กำหนด ตัวอย่าง 365 → 256 (2⁸) ใกล้กว่า 512 (2⁹) |365-256| = 109 < |512-365| = 147 ตัวอย่างโค้ด const nearestPowerOfTwo = num => { if (num < 0) num = -num; let base = 1; while (base < num) { if (num - base < base / 2) return base;

  7. หาพื้นที่สามเหลี่ยมด้วยสูตรเฮอรอน (Heron's Formula) - JavaScript

    คำนวณพื้นที่สามเหลี่ยมจากความยาว 3 ด้าน (a, b, c) โดยใช้ สูตรเฮอรอน (Herons Formula) ขั้นตอน หา s = กึ่งเส้นรอบรูป = (a + b + c) / 2 พื้นที่ = √[s(s-a)(s-b)(s-c)] ตัวอย่างโค้ด const sides = [12, 4, 9]; const areaOfTriangle = sides => { const [a, b, c] = sides; const s = (a + b + c) /

  8. หาคำที่สั้นที่สุดและยาวที่สุดในสตริง - JavaScript

    แยกสตริงเป็นคำ (split by space) แล้วหาคำที่สั้นที่สุดและยาวที่สุด ตัวอย่าง Hardships often prepare ordinary people for an extraordinary destiny → [an, extraordinary] ตัวอย่างโค้ด const str = Hardships often prepare ordinary people for an extraordinary destiny; const largestSmallest = str => {

  9. อ่านไฟล์ข้อความและนับจำนวนตัวอักษร - Node.js

    ใช้โมดูล fs (File System) ของ Node.js อ่านไฟล์ข้อความ แล้วนับความยาว (จำนวนตัวอักษร) ตัวอย่างโค้ด const fs = require(fs); const requireFile = async () => { const data = fs.readFileSync(./data.txt, utf-8); return data.length; }; requireFile() .then(res => console.log(res)) .catch(err

  10. หาค่ามากที่สุดจากหลายตัวเลข - JavaScript

    หาค่าสูงสุดจากตัวเลขหลายตัว ใช้ Rest Parameters (...nums) รับจำนวนอาร์กิวเมนต์ไม่จำกัด ตัวอย่างโค้ด max) max = nums[i]; } return max; }; console.log(findGreatest(5, 6, 3, 5, 7, 5)); ผลลัพธ์ 7 วิธีแบบ Built-in (แนะนำ) const findGreatest = (...nums) => Math.max(...nums); // หรือถ้ามีอาร์เ

  11. ความต่างระหว่างเส้นรอบวงและพื้นที่วงกลม - JavaScript

    คำนวณความต่างสัมบูรณ์ระหว่างพื้นที่และเส้นรอบวงของวงกลม สูตร พื้นที่ (Area) = π × r² เส้นรอบวง (Circumference) = 2 × π × r ตัวอย่างโค้ด const circleDifference = radius => { const area = Math.PI * radius * radius; const circumference = 2 * Math.PI * radius; return Math.abs(area - cir

  12. หาเลข Armstrong ในช่วง - JavaScript

    Armstrong Number (Narcissistic Number) คือเลขที่ผลรวมของแต่ละหลักยกกำลังจำนวนหลัก (n) เท่ากับตัวเลขเดิม สูตร xyz = xⁿ + yⁿ + zⁿ (n = จำนวนหลัก) ตัวอย่าง 370 = 3³ + 7³ + 0³ = 27 + 343 + 0 = 370 ✓ ตัวอย่างโค้ด const isArmstrong = number => { const len = String(number).length; let sum = 0, n

  13. แปลงวันเป็น ปี เดือน สัปดาห์ วัน - JavaScript

    แปลงจำนวนวันทั้งหมดเป็นออบเจกต์ที่มี years, months, weeks, days โดยไม่นับปีอธิกสุรทิน (ปีละ 365 วัน เดือนละ 30 วัน) ตัวอย่าง 738 days → { years: 2, months: 0, weeks: 1, days: 1 } ตัวอย่างโค้ด = 7) { weeks++; d -= 7; } else { days++; d--; } } return { years, months, weeks, days }; }; co

  14. ตรวจสอบเลข Disarium Number - JavaScript

    Disarium Number คือเลขที่ผลรวมของแต่ละหลักยกกำลังลำดับ (1, 2, 3...) เท่ากับตัวเลขเดิม สูตร xyz = x¹ + y² + z³ ตัวอย่าง 175 = 1¹ + 7² + 5³ = 1 + 49 + 125 = 175 ✓ ตัวอย่างโค้ด const isDisarium = num => { const sum = String(num) .split() .reduce((acc, val, ind) => acc + Math.pow(+val

  15. หาค่าซ้ำในอาร์เรย์ แต่ละค่าแสดงเพียงครั้งเดียว - JavaScript

    จากอาร์เรย์ที่มีค่าซ้ำหลายครั้ง ให้ดึงเฉพาะค่าที่ซ้ำ (ปรากฏมากกว่า 1 ครั้ง) แสดงแค่ครั้งเดียว ตัวอย่าง Input: [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4] Output: [1, 4, 3, 2] ตัวอย่างโค้ด const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4]; const pickDuplicate = arr => { const res = []; for (let i

  16. Transpose ของอาร์เรย์ 2 มิติ (Matrix Transpose) - JavaScript

    Transpose คือการสลับแถวกับคอลัมน์ของเมทริกซ์ (2D Array) แถวกลายเป็นคอลัมน์ คอลัมน์กลายเป็นแถว ตัวอย่าง Input: Output: [1, 1, 1] [1, 2, 3] [2, 2, 2] → [1, 2, 3] [3, 3, 3] [1, 2, 3] ตัวอย่างโค้ด (In-place สำหรับ Square Matrix) const arr = [ [1, 1, 1], [2, 2, 2], [3, 3,

  17. ตรวจสอบความเท่ากันของอาร์เรย์ 2 มิติ - JavaScript

    เปรียบเทียบอาร์เรย์ 2 มิติ (2D Arrays) ว่าเท่ากันทุกค่าหรือไม่ ต้องมีขนาดแถวและคอลัมน์เท่ากัน ตัวอย่างโค้ด const arr1 = [ [1, 1, 1], [2, 2, 2], [3, 3, 3] ]; const arr2 = [ [1, 1, 1], [2, 2, 2], [3, 3, 3] ]; const areEqual = (first, second) => { if (first.length !== second.length)

  18. นับเครื่องหมายวรรคตอนในสตริง - JavaScript

    นับจำนวนเครื่องหมายวรรคตอน (punctuation) ในสตริงภาษาอังกฤษ เครื่องหมายที่นับ ! , ; . - ? ตัวอย่างโค้ด const str = This, is a-sentence;.Is this a sentence?; const countPunctuation = str => { const punct = !,\;.-?; let count = 0; for (let i = 0; i < str.length; i++) { if (punct.in

  19. QuickSort แบบ Divide & Conquer - JavaScript

    การเรียงลำดับ QuickSort: อัลกอริทึมแบ่งแยกและเอาชนะ (Divide & Conquer) เลือก pivot แล้ววางค่าน้อยกว่าซ้าย มากกว่าขวา เรียกซ้ำ ตัวอย่างโค้ด const arr = [43, 3, 34, 34, 23, 232, 3434, 4, 23, 2, 54, 6, 54]; const quickSort = (arr, left = 0, right = arr.length - 1) => { if (left < right) {

Total 5941 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:97/298  20-คอมพิวเตอร์/Page Goto:1 91 92 93 94 95 96 97 98 99 100 101 102 103