Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript
JavaScript
  1. หาตัวเลขมากที่สุดใน 3D JavaScript Array

    ข้อมูลตัวอย่าง var theValuesIn3DArray = [75, [18, 89], [56, [97], [99]]]; วิธีทำ ใช้ flat(Infinity) รวมเป็น 1D array แล้ว Math.max(...array) ตัวอย่างโค้ด var theValuesIn3DArray = [75, [18, 89], [56, [97], [99]]]; Array.prototype.findTheLargestNumberIn3dArray = function (){ return Math.max(...thi

  2. ตรวจสอบ Undefined หรือ Null ใน JavaScript Array และแสดงเฉพาะค่าที่ไม่ใช่ Null

    ข้อมูลตัวอย่าง var firstName=[John,null,Mike,David,Bob,undefined]; วิธีทำ ใช้ for loop ตรวจสอบ != undefined (ครอบคลุม null ด้วยเพราะ ==) ตัวอย่างโค้ด var firstName=[John,null,Mike,David,Bob,undefined]; for(var index=0; index < firstName.length; index++){ if(firstName[index] != undefined) co

  3. ตั้งค่า innerHTML ด้วย JavaScript

    Syntax ที่ถูกต้อง document.getElementById(yourIdName).innerHTML = yourValue; ตัวอย่างโค้ด <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">

  4. ตรวจสอบ JavaScript Arrays ว่ามี Empty Strings หรือไม่

    ข้อมูลตัวอย่าง var studentDetails = new Array(); studentDetails[0] = John; studentDetails[1] = ; studentDetails[2] = Smith; studentDetails[3] = ; studentDetails[4] = UK; วิธีตรวจสอบ ใช้ for loop ตรวจสอบ array[index] == ตัวอย่างโค้ด var studentDetails = new Array(); studentDetails[0] = John;

  5. การ Rotate Array ใน JavaScript

    โจทย์ เขียนฟังก์ชัน rotate array โดยเลื่อน n elements Input: const arr = [12, 6, 43, 5, 7, 2, 5], n=3Output: [5, 7, 2, 5, 12, 6, 43] ตัวอย่างโค้ด { for(let i = 0; i = l){ return; }; for(let i = 0; i < n; i++){ rotateByOne(this); }; }; const a = [1,2,3,4,5,6,7]; a.rotateBy(2); cons

  6. นับจำนวน N-digit Numbers ที่ผลรวมตำแหน่งคู่/คี่ หารลงตัวใน JavaScript

    โจทย์ หาจำนวน N-digit numbers ที่ผลรวมหลักตำแหน่งคู่หาร A ลงตัว และผลรวมหลักตำแหน่งคี่หาร B ลงตัว ตัวอย่างโค้ด { let start = Math.pow(10, (n-1)); const end = Math.pow(10, n)-1; const res = []; while(start <= end){ const { sumEven, sumOdd } = indexSum(start); const condition =

  7. นับจำนวนตัวอักษรเฉพาะในประโยคด้วย For Loop, Break, Continue ใน JavaScript

    โจทย์ เขียนฟังก์ชันนับว่าตัวอักษรเฉพาะตัวปรากฏกี่ครั้งในประโยค ตัวอย่างโค้ด { let count = 0; for(let i = 0; i < str.length; i++){ if(str[i] !== char){ // ใช้ continue ข้ามไป iteration ถัดไป continue; }; // ถ้ามาถึงตรงนี้ str[i] และ char เหมือนกัน count++;

  8. ตรวจสอบ Co-prime Numbers ใน JavaScript

    นิยาม Co-prime numbers: 2 ตัวเลขที่ไม่มีเฉพาะตัวหารร่วมกัน (1 ไม่ใช่ prime) ตัวอย่าง:4 และ 5 → co-prime9 และ 14 → co-prime18 และ 35 → co-prime21 และ 57 → ไม่ใช่ co-prime (มี 3 เป็นเฉพาะตัวหารร่วม) ตัวอย่างโค้ด num2 ? num1 : num2; for(let ind = 2; ind < smaller; ind++){ const condition1 = nu

  9. Parent และ Child Classes สามารถมี Method ชื่อเดียวกันได้ไหมใน JavaScript

    คำตอบ ได้ Parent และ child classes สามารถมี method ชื่อเดียวกันได้ (Method Overriding) ตัวอย่างโค้ด class Parent { constructor(parentValue) { this.parentValue = parentValue; } // Parent class method name เหมือน Child Class method name showValues() { console.log(The parent metho

  10. ป้องกันการ Insert NULL Values ลง Table ด้วย JavaScript

    วิธีทำ ตรวจสอบเงื่อนไขก่อน insert โดยใช้ while loop ตรวจสอบ null เงื่อนไขตรวจสอบ NULL while(!(yourVariableName1==null || yourVariableName2==null || yourVariableName3==null...)){ // your statements } ตัวอย่างโค้ด <!DOCTYPE html> <html lang="en"> <head> <meta ch

  11. แปลง Buffer ให้เป็น Readable String ใน JavaScript

    วิธีทำ ใช้ toString(utf8) ตัวอย่างโค้ด var actualBufferObject = Buffer.from([John Smith], utf8); console.log(The actual buffer object=); console.log(JSON.stringify(actualBufferObject)); console.log(Get back the original object=); console.log(actualBufferObject.toString(utf8)); var myObjectValue

  12. JavaScript Outer Function Call และ Return Result

    วิธีทำ ใช้ return keyword สำหรับ outer function call ตัวอย่างโค้ด var substractMethod = function () { var firstValue = 1000, thirdValue = 200; var divideMethod = function () { var secondValue = 500; console.log(The result of divideMethod()= + (firstValue/secondValue)); return (f

  13. ย้ายตัวอักษรพิมพ์ใหญ่ทั้งหมดไปต้น String ใน JavaScript

    โจทย์ String: my name is JOHN SMITH ย้ายตัวอักษรพิมพ์ใหญ่ทั้งหมดไปต้น string วิธีทำ ใช้ sort() ร่วมกับ regex /[A-Z]/ ตัวอย่างโค้ด /[A-Z]/.test(value1) ? /[A-Z]/.test(value2) ? 0 : -1 : 0) .join( ); console.log(After moving the all capital letters at the beginning=); console.log(moveAllCapital

  14. ตรวจสอบค่า Object Property ที่เปลี่ยนแปลงและ Update ค่าอื่นใน JavaScript

    วิธีทำ ใช้ getter (get property) เพื่อตรวจจับการเข้าถึงค่า ตัวอย่างโค้ด class Student{ constructor(studentMarks1, studentMarks2){ this.studentMarks1 = studentMarks1; this.studentMarks2 = studentMarks2; var alteredValue = this; this.getValues = { get studentMarks1() {

  15. ตรวจสอบ Illegal Number ด้วย isNaN() ใน JavaScript

    ตัวอย่างโค้ด function multiplication(firstValue, secondValue, callback) { var res = firstValue * secondValue; var err = isNaN(res) ? Something is wrong in input parameter : undefined; callback(res, err); } multiplication(10, 50, function (result, error) { console.log(The multiplication

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