Skip to content

Commit 2c48407

Browse files
authored
More translation
Translate "bitwise trick" and "Getting a substring"
1 parent 063c09e commit 2c48407

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

1-js/05-data-types/03-string/article.md

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,13 @@ if (str.indexOf("Widget") != -1) {
310310
}
311311
```
312312

313-
#### The bitwise NOT trick
313+
#### Trik bitwise NOT
314314

315-
One of the old tricks used here is the [bitwise NOT](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_NOT) `~` operator. It converts the number to a 32-bit integer (removes the decimal part if exists) and then reverses all bits in its binary representation.
315+
Salah satu trik lama yang digunakan disini adalah operator [bitwise NOT](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_NOT) `~`. Operator ini mengubah angka menjadi integer 32-bit (menghilangkan bagian desimal jika ada) lalu menegasikan semua bit pada representasi binernya.
316316

317-
In practice, that means a simple thing: for 32-bit integers `~n` equals `-(n+1)`.
317+
Dalam praktik, hal tersebut berarti: untuk integer 32-bit `~n` sama dengan `-(n+1)`.
318318

319-
For instance:
319+
Sebagai contoh:
320320

321321
```js run
322322
alert( ~2 ); // -3, the same as -(2+1)
@@ -327,11 +327,11 @@ alert( ~-1 ); // 0, the same as -(-1+1)
327327
*/!*
328328
```
329329

330-
As we can see, `~n` is zero only if `n == -1` (that's for any 32-bit signed integer `n`).
330+
Seperti yang kita lihat, `~n` bernilai nol apabila `n == -1` (untuk semua signed integer `n`).
331331

332-
So, the test `if ( ~str.indexOf("...") )` is truthy only if the result of `indexOf` is not `-1`. In other words, when there is a match.
332+
Jadi, pengecekan di dalam `if ( ~str.indexOf("...") )` bernilai benar apabila hasil dari `indexOf` tidak bernilai `-1`. Dengan kata lain, jika kemunculan ditemukan.
333333

334-
People use it to shorten `indexOf` checks:
334+
Orang-orang menggunakannya untuk memperpendek pengecekan `indexOf`:
335335

336336
```js run
337337
let str = "Widget";
@@ -341,63 +341,63 @@ if (~str.indexOf("Widget")) {
341341
}
342342
```
343343

344-
It is usually not recommended to use language features in a non-obvious way, but this particular trick is widely used in old code, so we should understand it.
344+
Biasanya tidak direkomendasikan untuk menggunakan fitur bahasa dengan cara yang tidak jelas, tetapi trik ini biasa digunakan di kode yang kuno, jadi kita harus memahaminya.
345345

346-
Just remember: `if (~str.indexOf(...))` reads as "if found".
346+
Ingat: `if (~str.indexOf(...))` dibaca sebagai "if ditemukan".
347347

348-
To be precise though, as big numbers are truncated to 32 bits by `~` operator, there exist other numbers that give `0`, the smallest is `~4294967295=0`. That makes such check is correct only if a string is not that long.
348+
Untuk lebih detail, karena bilangan yang besar dipotong menjadi 32-bit oleh operator `~`, ada angka lain yang memberikan hasil `0`, angka yang terkecil yaitu `~4294967295=0`. Hal tersebut menyebabkan trik ini benar apabila string yang dites tidak sepanjang itu.
349349

350-
Right now we can see this trick only in the old code, as modern JavaScript provides `.includes` method (see below).
350+
Kita dapat melihat bahwa trik ini hanya digunakan di kode yang kuno, karena Javascript modern menyediakan method `.includes` (lihat di bawah).
351351

352352
### includes, startsWith, endsWith
353353

354-
The more modern method [str.includes(substr, pos)](mdn:js/String/includes) returns `true/false` depending on whether `str` contains `substr` within.
354+
Method yang lebih modern [str.includes(substr, pos)](mdn:js/String/includes) mengembalikan `true/false` tergantung dengan apakah `str` mengandung `substr` di dalamnya.
355355

356-
It's the right choice if we need to test for the match, but don't need its position:
356+
Ini adalah pilihan yang cocok apabila kita hanya perlu mengetes apakah `substr` ada, tetapi tidak memerlukan posisinya:
357357

358358
```js run
359359
alert( "Widget with id".includes("Widget") ); // true
360360

361361
alert( "Hello".includes("Bye") ); // false
362362
```
363363

364-
The optional second argument of `str.includes` is the position to start searching from:
364+
Parameter opsinal kedua dari `str.includes` adalah posisi dimana pencarian mulai dilakukan:
365365

366366
```js run
367367
alert( "Widget".includes("id") ); // true
368368
alert( "Widget".includes("id", 3) ); // false, from position 3 there is no "id"
369369
```
370370

371-
The methods [str.startsWith](mdn:js/String/startsWith) and [str.endsWith](mdn:js/String/endsWith) do exactly what they say:
371+
Method [str.startsWith](mdn:js/String/startsWith) dan [str.endsWith](mdn:js/String/endsWith) melakukan fungsi seperti namanya:
372372

373373
```js run
374374
alert( "Widget".startsWith("Wid") ); // true, "Widget" starts with "Wid"
375375
alert( "Widget".endsWith("get") ); // true, "Widget" ends with "get"
376376
```
377377

378-
## Getting a substring
378+
## Mengambil substring
379379

380-
There are 3 methods in JavaScript to get a substring: `substring`, `substr` and `slice`.
380+
Ada 3 cara untuk mengambil sebuah substring di Javascript: `substring`, `substr` dan `slice`.
381381

382382
`str.slice(start [, end])`
383-
: Returns the part of the string from `start` to (but not including) `end`.
383+
: Mengembalikan bagian dari string dari `start` sampai (tapi tidak termasuk) `end`.
384384

385-
For instance:
385+
Sebagai contoh:
386386

387387
```js run
388388
let str = "stringify";
389389
alert( str.slice(0, 5) ); // 'strin', the substring from 0 to 5 (not including 5)
390390
alert( str.slice(0, 1) ); // 's', from 0 to 1, but not including 1, so only character at 0
391391
```
392392

393-
If there is no second argument, then `slice` goes till the end of the string:
393+
Jika tidak ada parameter kedua, maka `slice` akan mengambil semua bagian dari `start` sampai akhir string:
394394

395395
```js run
396396
let str = "st*!*ringify*/!*";
397397
alert( str.slice(2) ); // ringify, from the 2nd position till the end
398398
```
399399

400-
Negative values for `start/end` are also possible. They mean the position is counted from the string end:
400+
Nilai negatif untuk `start/end` juga bisa digunakan. Nilai negatif berarti posisinya dihitung dari akhir string:
401401

402402
```js run
403403
let str = "strin*!*gif*/!*y";
@@ -407,11 +407,11 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
407407
```
408408

409409
`str.substring(start [, end])`
410-
: Returns the part of the string *between* `start` and `end`.
410+
: Mengembalikan bagian dari string *di antara* `start` dan `end`.
411411

412-
This is almost the same as `slice`, but it allows `start` to be greater than `end`.
412+
Method ini hampir sama dengan `slice`, tetapi nilai `start` boleh lebih besar daripada `end`.
413413

414-
For instance:
414+
Sebagai contoh:
415415

416416
```js run
417417
let str = "st*!*ring*/!*ify";
@@ -426,37 +426,38 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
426426

427427
```
428428

429-
Negative arguments are (unlike slice) not supported, they are treated as `0`.
429+
Parameter negatif tidak didukung (tidak seperti slice), mereka dianggap sebagai `0`.
430430

431431
`str.substr(start [, length])`
432-
: Returns the part of the string from `start`, with the given `length`.
432+
: Mengembalikan substring dari `start`, dengan panjang `length`.
433433

434-
In contrast with the previous methods, this one allows us to specify the `length` instead of the ending position:
434+
Dibandingkan dengan cara-cara sebelumnya, cara ini memperbolehkan kita untuk menyebutkan `length` alih-alih posisi akhir dari string:
435435

436436
```js run
437437
let str = "st*!*ring*/!*ify";
438438
alert( str.substr(2, 4) ); // ring, from the 2nd position get 4 characters
439439
```
440440

441-
The first argument may be negative, to count from the end:
441+
Parameter pertama mungkin bernilai negatif, untuk menghitung dari akhir string:
442442

443443
```js run
444444
let str = "strin*!*gi*/!*fy";
445445
alert( str.substr(-4, 2) ); // gi, from the 4th position get 2 characters
446446
```
447447

448-
Let's recap these methods to avoid any confusion:
448+
Mari kita review cara-cara tersebut untuk menghindari kebingungan:
449449

450-
| method | selects... | negatives |
450+
| method | mengambil... | negatives |
451451
|--------|-----------|-----------|
452-
| `slice(start, end)` | from `start` to `end` (not including `end`) | allows negatives |
453-
| `substring(start, end)` | between `start` and `end` | negative values mean `0` |
454-
| `substr(start, length)` | from `start` get `length` characters | allows negative `start` |
452+
| `slice(start, end)` | dari `start` sampai `end` (tidak termasuk `end`) | nilai negatif diperbolehkan |
453+
| `substring(start, end)` | antara `start` dan `end` | nilai negatif berarti mean `0` |
454+
| `substr(start, length)` | dari `start` ambil `length` karakter | `start` negatif diperbolehkan |
455455

456-
```smart header="Which one to choose?"
456+
```smart header="Cara mana yang harus kita gunakan?"
457457
All of them can do the job. Formally, `substr` has a minor drawback: it is described not in the core JavaScript specification, but in Annex B, which covers browser-only features that exist mainly for historical reasons. So, non-browser environments may fail to support it. But in practice it works everywhere.
458+
Semuanya dapat melakukan pekerjaannya. Secara formal, `substr` memiliki kekurangan: fungsi ini tidak tercantum di spesifikasi inti Javascript, tetapi di Annex B, yang mencakup hanya fitur browser yang ada karena alasan historis. Jadi, environment non-browser mungkin gagal untuk mendukungnya. Tetapi dalam praktik fungsi ini bekerja di mana saja.
458459
459-
Of the other two variants, `slice` is a little bit more flexible, it allows negative arguments and shorter to write. So, it's enough to remember solely `slice` of these three methods.
460+
Dibandingkan dengan dua varian yang lain, `slice` lebih fleksibel, karena memperbolehkan parameter negatif dan lebih pendek untuk ditulis. Jadi, dari ketiga cara sudah cukup untuk mengingat `slice`.
460461
```
461462

462463
## Comparing strings

0 commit comments

Comments
 (0)