Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions 1-js/05-data-types/04-array/1-item-value/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The result is `4`:
ผลลัพธ์คือ `4`:


```js run
Expand All @@ -13,5 +13,4 @@ alert( fruits.length ); // 4
*/!*
```

That's because arrays are objects. So both `shoppingCart` and `fruits` are the references to the same array.

เนื่องจากอาร์เรย์เป็นออบเจ็กต์ ทั้ง `shoppingCart` และ `fruits` จึงชี้ไปยังอาร์เรย์เดียวกัน
9 changes: 4 additions & 5 deletions 1-js/05-data-types/04-array/1-item-value/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ importance: 3

---

# Is array copied?
# อาร์เรย์ถูกคัดลอกหรือเปล่า?

What is this code going to show?
โค้ดนี้จะแสดงผลอะไร?

```js
let fruits = ["Apples", "Pear", "Orange"];

// push a new value into the "copy"
// push ค่าใหม่เข้าไปใน "สำเนา"
let shoppingCart = fruits;
shoppingCart.push("Banana");

// what's in fruits?
// แล้ว fruits มีอะไรอยู่?
alert( fruits.length ); // ?
```

44 changes: 22 additions & 22 deletions 1-js/05-data-types/04-array/10-maximal-subarray/solution.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
# Slow solution
# วิธีแก้แบบช้า

We can calculate all possible subsums.
เราสามารถคำนวณผลรวมของ subarray ทั้งหมดที่เป็นไปได้

The simplest way is to take every element and calculate sums of all subarrays starting from it.
วิธีที่ง่ายที่สุดคือ ดึงสมาชิกแต่ละตัวออกมา แล้วคำนวณผลรวมของ subarray ทุกชุดที่เริ่มต้นจากสมาชิกตัวนั้น

For instance, for `[-1, 2, 3, -9, 11]`:
เช่น สำหรับ `[-1, 2, 3, -9, 11]`:

```js no-beautify
// Starting from -1:
// เริ่มจาก -1:
-1
-1 + 2
-1 + 2 + 3
-1 + 2 + 3 + (-9)
-1 + 2 + 3 + (-9) + 11

// Starting from 2:
// เริ่มจาก 2:
2
2 + 3
2 + 3 + (-9)
2 + 3 + (-9) + 11

// Starting from 3:
// เริ่มจาก 3:
3
3 + (-9)
3 + (-9) + 11

// Starting from -9
// เริ่มจาก -9
-9
-9 + 11

// Starting from 11
// เริ่มจาก 11
11
```

The code is actually a nested loop: the external loop over array elements, and the internal counts subsums starting with the current element.
โค้ดที่ใช้จริงคือลูปซ้อนกัน: ลูปนอกวนผ่านสมาชิกในอาร์เรย์ ส่วนลูปในคำนวณผลรวม subarray ที่เริ่มจากสมาชิกตัวปัจจุบัน

```js run
function getMaxSubSum(arr) {
let maxSum = 0; // if we take no elements, zero will be returned
let maxSum = 0; // ถ้าไม่เลือกสมาชิกตัวใดเลย จะคืนค่าเป็นศูนย์

for (let i = 0; i < arr.length; i++) {
let sumFixedStart = 0;
Expand All @@ -57,25 +57,25 @@ alert( getMaxSubSum([1, 2, 3]) ); // 6
alert( getMaxSubSum([100, -9, 2, -3, 5]) ); // 100
```

The solution has a time complexity of [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation). In other words, if we increase the array size 2 times, the algorithm will work 4 times longer.
วิธีนี้มีความซับซ้อนของเวลาเป็น [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation) พูดง่ายๆ ก็คือ ถ้าขนาดอาร์เรย์เพิ่มขึ้น 2 เท่า อัลกอริทึมจะทำงานช้าลงถึง 4 เท่า

For big arrays (1000, 10000 or more items) such algorithms can lead to serious sluggishness.
สำหรับอาร์เรย์ขนาดใหญ่ (1,000, 10,000 สมาชิกขึ้นไป) วิธีแบบนี้อาจทำให้โปรแกรมทำงานช้ามาก

# Fast solution
# วิธีแก้แบบเร็ว

Let's walk the array and keep the current partial sum of elements in the variable `s`. If `s` becomes negative at some point, then assign `s=0`. The maximum of all such `s` will be the answer.
เราวนผ่านอาร์เรย์แล้วเก็บผลรวมสะสมของสมาชิกไว้ในตัวแปร `s` ถ้า `s` ติดลบ ณ จุดใดก็ตาม ให้กำหนด `s=0` ใหม่ ค่าสูงสุดของ `s` ตลอดการวนนั้นคือคำตอบ

If the description is too vague, please see the code, it's short enough:
ถ้าคำอธิบายยังไม่ชัดเจน ลองดูที่โค้ดได้เลย มันสั้นมาก:

```js run demo
function getMaxSubSum(arr) {
let maxSum = 0;
let partialSum = 0;

for (let item of arr) { // for each item of arr
partialSum += item; // add it to partialSum
maxSum = Math.max(maxSum, partialSum); // remember the maximum
if (partialSum < 0) partialSum = 0; // zero if negative
for (let item of arr) { // วนผ่านสมาชิกแต่ละตัวในอาร์เรย์
partialSum += item; // บวกเพิ่มเข้า partialSum
maxSum = Math.max(maxSum, partialSum); // จำค่าสูงสุดไว้
if (partialSum < 0) partialSum = 0; // รีเซ็ตเป็นศูนย์ถ้าติดลบ
}

return maxSum;
Expand All @@ -89,6 +89,6 @@ alert( getMaxSubSum([1, 2, 3]) ); // 6
alert( getMaxSubSum([-1, -2, -3]) ); // 0
```

The algorithm requires exactly 1 array pass, so the time complexity is O(n).
อัลกอริทึมนี้วนผ่านอาร์เรย์เพียงรอบเดียว ความซับซ้อนของเวลาจึงเป็น O(n)

You can find more detailed information about the algorithm here: [Maximum subarray problem](http://en.wikipedia.org/wiki/Maximum_subarray_problem). If it's still not obvious why that works, then please trace the algorithm on the examples above, see how it works, that's better than any words.
อ่านรายละเอียดเพิ่มเติมเกี่ยวกับอัลกอริทึมนี้ได้ที่: [Maximum subarray problem](http://en.wikipedia.org/wiki/Maximum_subarray_problem) ถ้ายังไม่เข้าใจว่าทำไมวิธีนี้ถึงได้ผล ลองลากการทำงานของอัลกอริทึมผ่านตัวอย่างข้างต้นดู การเห็นภาพจริงๆ ดีกว่าคำอธิบายใดๆ ทั้งนั้น
18 changes: 9 additions & 9 deletions 1-js/05-data-types/04-array/10-maximal-subarray/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ importance: 2

---

# A maximal subarray
# ผลรวมของ subarray ที่มากที่สุด

The input is an array of numbers, e.g. `arr = [1, -2, 3, 4, -9, 6]`.
อินพุตคืออาร์เรย์ของตัวเลข เช่น `arr = [1, -2, 3, 4, -9, 6]`

The task is: find the contiguous subarray of `arr` with the maximal sum of items.
โจทย์คือ: ให้หา subarray ที่ต่อเนื่องกันใน `arr` ซึ่งมีผลรวมของสมาชิกมากที่สุด

Write the function `getMaxSubSum(arr)` that will return that sum.
เขียนฟังก์ชัน `getMaxSubSum(arr)` ที่คืนค่าผลรวมนั้น

For instance:
ตัวอย่างเช่น:

```js
getMaxSubSum([-1, *!*2, 3*/!*, -9]) == 5 (the sum of highlighted items)
getMaxSubSum([-1, *!*2, 3*/!*, -9]) == 5 (ผลรวมของสมาชิกที่ไฮไลต์)
getMaxSubSum([*!*2, -1, 2, 3*/!*, -9]) == 6
getMaxSubSum([-1, 2, 3, -9, *!*11*/!*]) == 11
getMaxSubSum([-2, -1, *!*1, 2*/!*]) == 3
getMaxSubSum([*!*100*/!*, -9, 2, -3, 5]) == 100
getMaxSubSum([*!*1, 2, 3*/!*]) == 6 (take all)
getMaxSubSum([*!*1, 2, 3*/!*]) == 6 (นำทุกสมาชิกมารวม)
```

If all items are negative, it means that we take none (the subarray is empty), so the sum is zero:
ถ้าสมาชิกทุกตัวเป็นค่าลบ แสดงว่าเราไม่เลือกสมาชิกตัวใดเลย (subarray ว่างเปล่า) ผลรวมจึงเป็นศูนย์:

```js
getMaxSubSum([-1, -2, -3]) = 0
```

Please try to think of a fast solution: [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation) or even O(n) if you can.
ลองคิดหาวิธีที่เร็ว: [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation) หรือถ้าทำได้ก็ O(n) เลย
17 changes: 8 additions & 9 deletions 1-js/05-data-types/04-array/2-create-array/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ importance: 5

---

# Array operations.
# การดำเนินการกับอาร์เรย์

Let's try 5 array operations.
มาลองทำ 5 การดำเนินการกับอาร์เรย์กัน

1. Create an array `styles` with items "Jazz" and "Blues".
2. Append "Rock-n-Roll" to the end.
3. Replace the value in the middle with "Classics". Your code for finding the middle value should work for any arrays with odd length.
4. Strip off the first value of the array and show it.
5. Prepend `Rap` and `Reggae` to the array.
1. สร้างอาร์เรย์ `styles` ที่มีสมาชิก "Jazz" และ "Blues"
2. เพิ่ม "Rock-n-Roll" ไว้ที่ท้ายอาร์เรย์
3. แทนค่าตรงกลางด้วย "Classics" โดยโค้ดที่ใช้หาตำแหน่งกลางต้องทำงานได้กับอาร์เรย์ที่มีความยาวเป็นเลขคี่ทุกขนาด
4. ตัดสมาชิกตัวแรกออกจากอาร์เรย์แล้วแสดงผลค่านั้น
5. เพิ่ม `Rap` และ `Reggae` ไว้ที่ต้นอาร์เรย์

The array in the process:
ลำดับการเปลี่ยนแปลงของอาร์เรย์:

```js no-beautify
Jazz, Blues
Expand All @@ -21,4 +21,3 @@ Jazz, Classics, Rock-n-Roll
Classics, Rock-n-Roll
Rap, Reggae, Classics, Rock-n-Roll
```

6 changes: 3 additions & 3 deletions 1-js/05-data-types/04-array/3-call-array-this/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The call `arr[2]()` is syntactically the good old `obj[method]()`, in the role of `obj` we have `arr`, and in the role of `method` we have `2`.
การเรียก `arr[2]()` นั้นในเชิงไวยากรณ์ก็คือรูปแบบ `obj[method]()` ที่คุ้นเคย โดย `arr` ทำหน้าที่เป็น `obj` และ `2` ทำหน้าที่เป็น `method`

So we have a call of the function `arr[2]` as an object method. Naturally, it receives `this` referencing the object `arr` and outputs the array:
ดังนั้นจึงเป็นการเรียกฟังก์ชัน `arr[2]` ในฐานะเมธอดของออบเจ็กต์ ซึ่งแน่นอนว่า this จะชี้ไปยังออบเจ็กต์ `arr` และแสดงผลเป็นอาร์เรย์:

```js run
let arr = ["a", "b"];
Expand All @@ -12,4 +12,4 @@ arr.push(function() {
arr[2](); // a,b,function(){...}
```

The array has 3 values: initially it had two, plus the function.
อาร์เรย์มีค่าอยู่ 3 ค่า: เดิมมีสองค่า บวกกับฟังก์ชันที่เพิ่งเพิ่มเข้ามา
5 changes: 2 additions & 3 deletions 1-js/05-data-types/04-array/3-call-array-this/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# Calling in an array context
# การเรียกในบริบทของอาร์เรย์

What is the result? Why?
ผลลัพธ์คืออะไร และทำไม?

```js
let arr = ["a", "b"];
Expand All @@ -15,4 +15,3 @@ arr.push(function() {

arr[2](); // ?
```

11 changes: 5 additions & 6 deletions 1-js/05-data-types/04-array/5-array-input-sum/solution.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Please note the subtle, but important detail of the solution. We don't convert `value` to number instantly after `prompt`, because after `value = +value` we would not be able to tell an empty string (stop sign) from the zero (valid number). We do it later instead.
สังเกตรายละเอียดเล็กน้อยแต่สำคัญในโซลูชันนี้ เราไม่แปลง `value` เป็นตัวเลขทันทีหลังจาก `prompt` เพราะถ้าทำ `value = +value` ไปก่อน เราจะไม่สามารถแยกแยะสตริงว่าง (สัญญาณหยุด) ออกจากเลขศูนย์ (ค่าที่ถูกต้อง) ได้ จึงแปลงทีหลังแทน


```js run demo
function sumInput() {

let numbers = [];

while (true) {

let value = prompt("A number please?", 0);
let value = prompt("กรุณาป้อนตัวเลข", 0);

// should we cancel?
// ควรหยุดหรือเปล่า?
if (value === "" || value === null || !isFinite(value)) break;

numbers.push(+value);
Expand All @@ -23,6 +23,5 @@ function sumInput() {
return sum;
}

alert( sumInput() );
alert( sumInput() );
```

12 changes: 6 additions & 6 deletions 1-js/05-data-types/04-array/5-array-input-sum/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ importance: 4

---

# Sum input numbers
# รับตัวเลขแล้วรวมผล

Write the function `sumInput()` that:
เขียนฟังก์ชัน `sumInput()` ที่:

- Asks the user for values using `prompt` and stores the values in the array.
- Finishes asking when the user enters a non-numeric value, an empty string, or presses "Cancel".
- Calculates and returns the sum of array items.
- ถามค่าจากผู้ใช้ด้วย `prompt` แล้วเก็บค่าเหล่านั้นไว้ในอาร์เรย์
- หยุดถามเมื่อผู้ใช้ป้อนค่าที่ไม่ใช่ตัวเลข สตริงว่าง หรือกด "Cancel"
- คำนวณแล้วคืนค่าผลรวมของสมาชิกในอาร์เรย์

P.S. A zero `0` is a valid number, please don't stop the input on zero.
หมายเหตุ: เลข `0` ถือว่าเป็นตัวเลขที่ถูกต้อง อย่าหยุดรับค่าเมื่อผู้ใช้ป้อนศูนย์

[demo]
Loading