diff --git a/1-js/02-first-steps/13-while-for/1-loop-last-value/solution.md b/1-js/02-first-steps/13-while-for/1-loop-last-value/solution.md index 43ee4aad3..11a87cff2 100644 --- a/1-js/02-first-steps/13-while-for/1-loop-last-value/solution.md +++ b/1-js/02-first-steps/13-while-for/1-loop-last-value/solution.md @@ -1,4 +1,4 @@ -The answer: `1`. +Պատասխան՝ `1`։ ```js run let i = 3; @@ -8,18 +8,18 @@ while (i) { } ``` -Every loop iteration decreases `i` by `1`. The check `while(i)` stops the loop when `i = 0`. +Ցիկլի ամեն իտերացիա `i`֊ն նվազեցնում է `1`֊ով։ `while(i)` ստուգումը կանգ կառնի, երբ `i = 0`։ -Hence, the steps of the loop form the following sequence ("loop unrolled"): +Հետևաբար ցիկլը բաղկացած կլինի հետևյալ քայլերից ("բացենք ցիկլը")․ ```js let i = 3; -alert(i--); // shows 3, decreases i to 2 +alert(i--); // ցույց կտա 3, կնվազեցնի i֊ն 1֊ով՝ դարձնելով 2 -alert(i--) // shows 2, decreases i to 1 +alert(i--) // ցույց կտա 2, կնվազեցնի i֊ն 1֊ով՝ դարձնելով 1 -alert(i--) // shows 1, decreases i to 0 +alert(i--) // ցույց կտա 1, կնվազեցնի i֊ն 1֊ով՝ դարձնելով 0 -// done, while(i) check stops the loop +// վերջ, while(i) ստուգումը կդադարեցնի ցիկլը ``` diff --git a/1-js/02-first-steps/13-while-for/1-loop-last-value/task.md b/1-js/02-first-steps/13-while-for/1-loop-last-value/task.md index 3b847dfa2..6254eb49e 100644 --- a/1-js/02-first-steps/13-while-for/1-loop-last-value/task.md +++ b/1-js/02-first-steps/13-while-for/1-loop-last-value/task.md @@ -2,9 +2,9 @@ importance: 3 --- -# Last loop value +# Ցիկլի վերջին արժեքը -What is the last value alerted by this code? Why? +Ի՞նչ արժեք ցույց կտա այս ծրագիրը ամենավերջում և ինչու՞։ ```js let i = 3; diff --git a/1-js/02-first-steps/13-while-for/2-which-value-while/solution.md b/1-js/02-first-steps/13-while-for/2-which-value-while/solution.md index 495359876..4e7016d58 100644 --- a/1-js/02-first-steps/13-while-for/2-which-value-while/solution.md +++ b/1-js/02-first-steps/13-while-for/2-which-value-while/solution.md @@ -1,30 +1,30 @@ -The task demonstrates how postfix/prefix forms can lead to different results when used in comparisons. +Այս առաջադրանքը ցուց է տալիս, թե ինչպես նախածանցի/վերջածանցի կիրառումը կարող է բերել տարբեր արդյունքների, երբ դրանք օգտագործվում են համեմատության մեջ։ -1. **From 1 to 4** +1. **1֊ից մինչև 4** ```js run let i = 0; while (++i < 5) alert( i ); ``` - The first value is `i = 1`, because `++i` first increments `i` and then returns the new value. So the first comparison is `1 < 5` and the `alert` shows `1`. + Առաջին արժեքը `i = 1`, քանի որ `++i`֊ն սկզբում աճեցնում է `i`֊ի արժեքը, և հետո նոր վերադարձնում ստացված նոր աժեքը։ Այսպիսով առաջին համեմատությունը կլինի `1 < 5` և `alert`֊ը ցույց կտա `1`։ - Then follow `2, 3, 4…` -- the values show up one after another. The comparison always uses the incremented value, because `++` is before the variable. + Ապա կհաջորդի `2, 3, 4…` -- արժեքները կցուցադրվեն մեկը մյուսի հետևից։ Համեմատումը միշտ օգտագործում է ավելացված արժեքը, քանի որ `++`֊ը գրված է փոփոխականից առաջ։ - Finally, `i = 4` is incremented to `5`, the comparison `while(5 < 5)` fails, and the loop stops. So `5` is not shown. -2. **From 1 to 5** + Եվ վերջապես `i = 4` ավելացվում է դառնալով `5`, `while(5 < 5)` համեմատումը կլինի սխալ, և ցիկլը կավարտվի։ Այսպիսով `5`֊ը չի երևա էկրանին։ +2. **1֊ից մինչև 5** ```js run let i = 0; while (i++ < 5) alert( i ); ``` - The first value is again `i = 1`. The postfix form of `i++` increments `i` and then returns the *old* value, so the comparison `i++ < 5` will use `i = 0` (contrary to `++i < 5`). + Առաջին արժեքը կրկին `i = 1` է։ `i++`֊ը աճեցնում է `i`֊ն, և ապա վերադարձնում *հին* արժեքը, այսպիսով `i++ < 5` համեմատության մեջ `i = 0` (ի տարբերություն `++i < 5`֊ի)։ - But the `alert` call is separate. It's another statement which executes after the increment and the comparison. So it gets the current `i = 1`. + Բայց `alert`֊ի կանչը առանձին է։ Այն առանձին հատված է, որը կատարվում է փոփոխականի աճից և համեմատումից հետո։ Այսպիսով այն ստանում է ընթացիկ `i = 1`։ - Then follow `2, 3, 4…` + Ապա հետևում են `2, 3, 4…`։ - Let's stop on `i = 4`. The prefix form `++i` would increment it and use `5` in the comparison. But here we have the postfix form `i++`. So it increments `i` to `5`, but returns the old value. Hence the comparison is actually `while(4 < 5)` -- true, and the control goes on to `alert`. + Կանգ առնենք `i = 4` դեպքի վրա։ `++i`֊ն կաճեցներ `i`֊ն և կօգտագործեր `5` արժեքը համեմատման համար։ Բայց այստեղ օգտագործվում է `i++`, որը աճեցնում է `i`֊ն՝ դանձնելով `5`, և վերադարձնում հին արժեքը։ Հետևաբար համեմատումը կլինի `while(4 < 5)` -- որն էլ ճիշտ է, և `alert`֊ը ցույց կտա `5` արժեքը։ - The value `i = 5` is the last one, because on the next step `while(5 < 5)` is false. + `i = 5` արժեքի դեպքում հաջորդ քայլում կունենանք `while(5 < 5)`, որն էլ սխալ է։ diff --git a/1-js/02-first-steps/13-while-for/2-which-value-while/task.md b/1-js/02-first-steps/13-while-for/2-which-value-while/task.md index 298213237..21374d7fa 100644 --- a/1-js/02-first-steps/13-while-for/2-which-value-while/task.md +++ b/1-js/02-first-steps/13-while-for/2-which-value-while/task.md @@ -2,19 +2,19 @@ importance: 4 --- -# Which values does the while loop show? +# Ինչպիսի՞ արժեքներ ցույց կտա while ցիկլը -For every loop iteration, write down which value it outputs and then compare it with the solution. +Ամեն իտերացիայի համար գրառեք, թե ինչպիսի արժեքներ ցույց կտա ցիկլը, և համեմատեք ձեր պատասխանները լուծման հետ։ -Both loops `alert` the same values, or not? +Արդյո՞ք երկու ցիկլերն էլ ցույց `alert` կանեն նույն արժեքները, թե ոչ։ -1. The prefix form `++i`: +1. `++i` նախածանցով (prefix). ```js let i = 0; while (++i < 5) alert( i ); ``` -2. The postfix form `i++` +2. `i++` վերջածանցով (postfix). ```js let i = 0; diff --git a/1-js/02-first-steps/13-while-for/3-which-value-for/solution.md b/1-js/02-first-steps/13-while-for/3-which-value-for/solution.md index e2e28e75b..7bc9ef467 100644 --- a/1-js/02-first-steps/13-while-for/3-which-value-for/solution.md +++ b/1-js/02-first-steps/13-while-for/3-which-value-for/solution.md @@ -1,4 +1,4 @@ -**The answer: from `0` to `4` in both cases.** +**Պատասխան՝ `0`֊ից մինչև `4`, երկու դեպքում էլ։** ```js run for (let i = 0; i < 5; ++i) alert( i ); @@ -6,12 +6,12 @@ for (let i = 0; i < 5; ++i) alert( i ); for (let i = 0; i < 5; i++) alert( i ); ``` -That can be easily deducted from the algorithm of `for`: +Այս պատասխանին կարող ենք հասնել դիտարկելով `for`֊ի աշխատելու ալգորիթմը․ -1. Execute once `i = 0` before everything (begin). -2. Check the condition `i < 5` -3. If `true` -- execute the loop body `alert(i)`, and then `i++` +1. Կատարում է մի անգամ `i = 0`, ամեն ինչից առաջ (սկիզբ)։ +2. Ստուգում է պայմանը `i < 5`։ +3. Եթե ճիշտ է (`true`) -- կատարում է ցիկլի մարմինը՝ `alert(i)`, և ապա `i++` -The increment `i++` is separated from the condition check (2). That's just another statement. +`i++` աճը բաժանված է պայմանի ստուգման գործողությունից (2)։ Այն կոդի ուրիշ անկախ հատված է։ -The value returned by the increment is not used here, so there's no difference between `i++` and `++i`. +Աճեցումից հետո վերադարձված արժեքը այստեղ չի օգտագործվում, այսպիսով տարբերություն չկա `i++`֊ի և `++i`֊ի միջև։ diff --git a/1-js/02-first-steps/13-while-for/3-which-value-for/task.md b/1-js/02-first-steps/13-while-for/3-which-value-for/task.md index bfefa63f5..4b00882b9 100644 --- a/1-js/02-first-steps/13-while-for/3-which-value-for/task.md +++ b/1-js/02-first-steps/13-while-for/3-which-value-for/task.md @@ -2,18 +2,18 @@ importance: 4 --- -# Which values get shown by the "for" loop? +# Ո՞ր արժեքները կցուցադրվեն "for" ցիկլի միջոցով -For each loop write down which values it is going to show. Then compare with the answer. +Ամեն ցիկլի համար գրառեք, թե ինչպիսի արժեքներ ցույց կտա ցիկլը, և համեմատեք ձեր պատասխանները լուծման հետ։ -Both loops `alert` same values or not? +Արդյո՞ք երկու ցիկլերն էլ ցույց `alert` կանեն նույն արժեքները, թե ոչ։ -1. The postfix form: +1. Վերջածանցով (postfix). ```js for (let i = 0; i < 5; i++) alert( i ); ``` -2. The prefix form: +2. Նախածանցով (prefix). ```js for (let i = 0; i < 5; ++i) alert( i ); diff --git a/1-js/02-first-steps/13-while-for/4-for-even/solution.md b/1-js/02-first-steps/13-while-for/4-for-even/solution.md index e8e66bb47..c3c9d199c 100644 --- a/1-js/02-first-steps/13-while-for/4-for-even/solution.md +++ b/1-js/02-first-steps/13-while-for/4-for-even/solution.md @@ -8,4 +8,4 @@ for (let i = 2; i <= 10; i++) { } ``` -We use the "modulo" operator `%` to get the remainder and check for the evenness here. +Մենք օգտագործում ենք "մնացորդի" օպերատոր `%`֊ը մնացորդը ստանալու համար, և ստուգում ենք թվի զույգությունը։ diff --git a/1-js/02-first-steps/13-while-for/4-for-even/task.md b/1-js/02-first-steps/13-while-for/4-for-even/task.md index ff34e7e40..b4d8bfa20 100644 --- a/1-js/02-first-steps/13-while-for/4-for-even/task.md +++ b/1-js/02-first-steps/13-while-for/4-for-even/task.md @@ -2,8 +2,8 @@ importance: 5 --- -# Output even numbers in the loop +# Ցուցադրել զույգ թվերը ցիկլի միջոցով -Use the `for` loop to output even numbers from `2` to `10`. +Օգտագործեք `for` ցիկլը ցուցադրելու զույգ թվերը `2`֊ից մինչև `10`։ [demo] diff --git a/1-js/02-first-steps/13-while-for/5-replace-for-while/solution.md b/1-js/02-first-steps/13-while-for/5-replace-for-while/solution.md index 612cf559c..1d918d3d0 100644 --- a/1-js/02-first-steps/13-while-for/5-replace-for-while/solution.md +++ b/1-js/02-first-steps/13-while-for/5-replace-for-while/solution.md @@ -3,7 +3,7 @@ ```js run let i = 0; while (i < 3) { - alert( `number ${i}!` ); + alert( `Համար ${i}!` ); i++; } ``` diff --git a/1-js/02-first-steps/13-while-for/5-replace-for-while/task.md b/1-js/02-first-steps/13-while-for/5-replace-for-while/task.md index 0c69d9c2d..924ca648d 100644 --- a/1-js/02-first-steps/13-while-for/5-replace-for-while/task.md +++ b/1-js/02-first-steps/13-while-for/5-replace-for-while/task.md @@ -2,13 +2,13 @@ importance: 5 --- -# Replace "for" with "while" +# Փոխարինեք "for"֊ը "while"֊ով -Rewrite the code changing the `for` loop to `while` without altering its behavior (the output should stay same). +Վերաշարադրեք կոդը, փոխելով `for` ցիկլը `while`֊ով, առանց նրա կատարման արդյունքը փոխելու (ցուցադրվող արժեքները պետք է մնան նույնը)։ ```js run for (let i = 0; i < 3; i++) { - alert( `number ${i}!` ); + alert( `Համար ${i}!` ); } ``` diff --git a/1-js/02-first-steps/13-while-for/6-repeat-until-correct/solution.md b/1-js/02-first-steps/13-while-for/6-repeat-until-correct/solution.md index c7de5f09b..061e91102 100644 --- a/1-js/02-first-steps/13-while-for/6-repeat-until-correct/solution.md +++ b/1-js/02-first-steps/13-while-for/6-repeat-until-correct/solution.md @@ -3,13 +3,13 @@ let num; do { - num = prompt("Enter a number greater than 100?", 0); + num = prompt("Ներմուծեք 100֊ից բարձր թիվ", 0); } while (num <= 100 && num); ``` -The loop `do..while` repeats while both checks are truthy: +`do..while` ցիկլը կրկնում է քանի դեռ երկու պայմաններն էլ ճշմարիտ են․ -1. The check for `num <= 100` -- that is, the entered value is still not greater than `100`. -2. The check `&& num` is false when `num` is `null` or an empty string. Then the `while` loop stops too. +1. `num <= 100` ստուգումը նշանակում է, որ ներմուծված թիվը դեռ մեծ չէ `100`֊ից։ +2. `&& num` ստուգումը սխալ է, երբ `num`֊ը `null` է կամ դատարկ տող։ Այդ դեպքում `while`֊ը կանգ է առնում։ -P.S. If `num` is `null` then `num <= 100` is `true`, so without the 2nd check the loop wouldn't stop if the user clicks CANCEL. Both checks are required. +Եթե `num`֊ը `null` է, ապա `num <= 100` համեմատումը `true` է, այսինքն առանց երկրորդ պայմանի ցիկլը կանգ չէր առնի, եթե օգտատերը սեղմեր ՉԵՂԱՐԿԵԼ (CANCEL): Երկու պայմանն էլ պարտադիր են։ diff --git a/1-js/02-first-steps/13-while-for/6-repeat-until-correct/task.md b/1-js/02-first-steps/13-while-for/6-repeat-until-correct/task.md index 0788ee76e..ca3b03c69 100644 --- a/1-js/02-first-steps/13-while-for/6-repeat-until-correct/task.md +++ b/1-js/02-first-steps/13-while-for/6-repeat-until-correct/task.md @@ -2,12 +2,12 @@ importance: 5 --- -# Repeat until the input is correct +# Կրկնել քանի դեռ ներմուծված արժեքը ճիշտ չէ -Write a loop which prompts for a number greater than `100`. If the visitor enters another number -- ask them to input again. +Գրել ցիկլ, որը կհարցնի (prompt) թիվ, որը պետք է լինի մեծ `100`֊ից։ Եթե օգտատերը ներմուծի ուրիշ թիվ (պայմանին չբավարարող) -- տալ նրան ներմուծել կրկին։ -The loop must ask for a number until either the visitor enters a number greater than `100` or cancels the input/enters an empty line. +Ցիկլը պետք է ուզի թիվ օգտատերից, մինչև մուտքագրվի `100`֊ից մեծ թիվ կամ չեղարկվի (cancel) ներմուծումը (մուտքագրվի դատարկ տող)։ -Here we can assume that the visitor only inputs numbers. There's no need to implement a special handling for a non-numeric input in this task. +Ենթադրվում է, որ օգտատերը ներմուծում է միայն թվային արժեքներ։ Լրացուցիչ դեպքեր պետք չէ դիտարկել ոչ թվային արժեքների համար։ [demo] diff --git a/1-js/02-first-steps/13-while-for/7-list-primes/solution.md b/1-js/02-first-steps/13-while-for/7-list-primes/solution.md index b4b64b6fa..73aae6486 100644 --- a/1-js/02-first-steps/13-while-for/7-list-primes/solution.md +++ b/1-js/02-first-steps/13-while-for/7-list-primes/solution.md @@ -1,29 +1,29 @@ -There are many algorithms for this task. +Կան շատ տարբեր ալգորիթմներ այս խնդրիը լուծելու համար։ -Let's use a nested loop: +Եկեք օգտագործենք ներդրված ցիկլեր․ ```js -For each i in the interval { - check if i has a divisor from 1..i - if yes => the value is not a prime - if no => the value is a prime, show it +Բոլոր i֊երի համար հատվածից { + ստուգում է, արդյոք i֊ն բաժանվում է ինչ֊որ թվի միչև ինքը + եթե այո => արժեքը պարզ չէ + եթե ոչ => արժեքը պարզ է և պետք է ցույց տալ այն } ``` -The code using a label: +Ծրագիրը օգտագործում է պիտակ (label)․ ```js run let n = 10; nextPrime: -for (let i = 2; i <= n; i++) { // for each i... +for (let i = 2; i <= n; i++) { // ամեն i֊ի համար... - for (let j = 2; j < i; j++) { // look for a divisor.. - if (i % j == 0) continue nextPrime; // not a prime, go next i + for (let j = 2; j < i; j++) { // փնտրում է բաժանարար.. + if (i % j == 0) continue nextPrime; // պարզ չէ, անցնում է հաջորդ i֊ին } - alert( i ); // a prime + alert( i ); // պարզ թիվ } ``` -There's a lot of space to optimize it. For instance, we could look for the divisors from `2` to square root of `i`. But anyway, if we want to be really efficient for large intervals, we need to change the approach and rely on advanced maths and complex algorithms like [Quadratic sieve](https://en.wikipedia.org/wiki/Quadratic_sieve), [General number field sieve](https://en.wikipedia.org/wiki/General_number_field_sieve) etc. +Այս օրինակում կան շատ տեղեր, որ կարելի է ավելի օպտիմալ գրել։ Օրինակ մենք կարող ենք փնտրել բաժանարարները `2`֊ից մինչև արմատ `i`֊ն։ Մեծ ինտերվալների դեպքում մենք պետք է լավացնենք (այսինքն փոքրացնենք) փնտրման սահմանները, ավելի արդյունավետ ծրագիր ստանալու համար։ Դրա համար պետք է ծանոթ լինենք խորացված մաթեմատիկայի տարրերին և կոմպլեքս ալգորիթմներին, ինչպիսիք են [Քառակուսային մաղ (Quadratic sieve)](https://en.wikipedia.org/wiki/Quadratic_sieve), [Ընդհանուր թվերի դաշտային մաղ (General number field sieve)](https://en.wikipedia.org/wiki/General_number_field_sieve) և այլն։ diff --git a/1-js/02-first-steps/13-while-for/7-list-primes/task.md b/1-js/02-first-steps/13-while-for/7-list-primes/task.md index 6344b9f6f..0c0c56b83 100644 --- a/1-js/02-first-steps/13-while-for/7-list-primes/task.md +++ b/1-js/02-first-steps/13-while-for/7-list-primes/task.md @@ -2,16 +2,16 @@ importance: 3 --- -# Output prime numbers +# Տպել պարզ թվերը -An integer number greater than `1` is called a [prime](https://en.wikipedia.org/wiki/Prime_number) if it cannot be divided without a remainder by anything except `1` and itself. +`1`֊ից մեծ ամբողջ թիվը կոչվում է [պարզ](https://hy.wikipedia.org/wiki/Պարզ_թիվ), եթե ունի միայն երկու բաժանարար, դրանք են՝ `1`֊ը և հենց ինքը։ -In other words, `n > 1` is a prime if it can't be evenly divided by anything except `1` and `n`. +Այլ կերպ ասած, `n > 1` պայմանին բավարարող `n`֊ը պարզ է, եթե այն հնարավոր չէ անմնացորդ բաժանել կամայական ամբողջ թվի վրա, բացի `1`֊ից և `n`֊ից։ -For example, `5` is a prime, because it cannot be divided without a remainder by `2`, `3` and `4`. +Օրինակ, `5`֊ը պարզ է, քանի որ այն չի բաժանվում `2`֊ի, `3`֊ի և `4`֊ի։ -**Write the code which outputs prime numbers in the interval from `2` to `n`.** +**Գրել ծրագիր, որը կտպի բոլոր պարզ թվերը `2`֊ից մինչև `n` հատվածում:** -For `n = 10` the result will be `2,3,5,7`. +`n = 10` դեպքի համար արդյունքը պետք է լինի `2,3,5,7`։ -P.S. The code should work for any `n`, not be hard-tuned for any fixed value. +Հ․Գ․ Ծրագիրը պետք է աշխատի կամայական `n`֊ի համար, այսինքն `n`֊ը պետք է հեշտ մոդիֆիկացվի։ diff --git a/1-js/02-first-steps/13-while-for/article.md b/1-js/02-first-steps/13-while-for/article.md index a7a211569..20a75efca 100644 --- a/1-js/02-first-steps/13-while-for/article.md +++ b/1-js/02-first-steps/13-while-for/article.md @@ -1,54 +1,54 @@ -# Loops: while and for +# Ցիկլեր․ while և for -We often need to repeat actions. +Հաճախ մենք կարիք ենք ունենում կրկնել ինչ֊որ գործողությունների հերթականություն։ -For example, outputting goods from a list one after another or just running the same code for each number from 1 to 10. +Օրինակ․ տպել իրար հետևից ցուցակի մեջ գտնվող ապրանքները, կամ ուղղակի աշխատեցնել նույն կոդը 1֊ից 10 բոլոր թվերի համար։ -*Loops* are a way to repeat the same code multiple times. +*Ցիկլերը* նույն կոդը մի քանի անգամ կրկնելու հնարավորություն են տալիս։ -## The "while" loop +## "while" ցիկլը -The `while` loop has the following syntax: +`while` ցիկլը ունի հետևյալ գրելաձևը․ ```js while (condition) { - // code - // so-called "loop body" + // կոդը + // որին նաև ասում են "ցիկլի մարմին" ("loop body") } ``` -While the `condition` is truthy, the `code` from the loop body is executed. +Քանի դեռ `condition`֊ը (պայմանը) ճշմարիտ է, `կոդը` ցիկլի մարմնի ներսում կկատարվի։ -For instance, the loop below outputs `i` while `i < 3`: +Օրինակի համար, ներքևի ցիկլը կարտածի `i`, քանի դեռ `i < 3`․ ```js run let i = 0; -while (i < 3) { // shows 0, then 1, then 2 +while (i < 3) { // ցույց կտա 0, հետո 1, հետո 2 alert( i ); i++; } ``` -A single execution of the loop body is called *an iteration*. The loop in the example above makes three iterations. +Ցիկլի մարմնի մի կատարումը անվանում են *իտերացիա* (*an iteration*): Ցիկլը վերևի օրինակում կատարում է երեք իտերացիա։ -If `i++` was missing from the example above, the loop would repeat (in theory) forever. In practice, the browser provides ways to stop such loops, and in server-side JavaScript, we can kill the process. +Եթե չլիներ `i++` գործողությունը վերևի օրինակում, ապա ցիկլը կկրկնվեր (տեսականորեն) անվերջ։ Պրակտիկայում, դիտարկիչը (browser) կտրամադրի տարբեր գործիքներ, որոնց միջոցով կկարողանանք կանգնեցնել այդպիսի ցիկլկերը, և սերվերում (server-side) կիրառվող JavaScript֊ում կարող ենք սպանել (kill) այդ պրոցեսը։ -Any expression or variable can be a loop condition, not just comparisons: the condition is evaluated and converted to a boolean by `while`. +Կամայական արտահայտություն կամ փոփոխական կարող է լինել ցիկլի պայման։ Պայմանը հաշվվում և ձևափոխվում է տրամաբանական տիպի `while`֊ի կողմից։ -For instance, a shorter way to write `while (i != 0)` is `while (i)`: +Օրինակի համար, `while (i != 0)` արտահայտության կարճ գրելաձևը կլինի `while (i)`: ```js run let i = 3; *!* -while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops +while (i) { // երբ i֊ն դառնա 0, պայմանը կդառնա սխալական, և ցիկլը կանգ կառնի */!* alert( i ); i--; } ``` -````smart header="Curly braces are not required for a single-line body" -If the loop body has a single statement, we can omit the curly braces `{…}`: +````smart header="Ձևավոր փակագծերը պարտադիր չեն մեկ տողանի մարմնի համար" +Եթե ցիկլի մարմինը ունի ընդամենը մեկ արտահայտություն (statement), ապա կարող ենք բաց թողնել ձևավոր փակագծերը `{…}`․ ```js run let i = 3; @@ -58,19 +58,19 @@ while (i) alert(i--); ``` ```` -## The "do..while" loop +## "do..while" ցիկլը -The condition check can be moved *below* the loop body using the `do..while` syntax: +Պայմանի ստուգումը կարելի է տեղափոխել ցիկլի մարմնի *ներքևը* օգտագործելով `do..while` գերլաձևը․ ```js do { - // loop body + // ցիկլի մարմին } while (condition); ``` -The loop will first execute the body, then check the condition, and, while it's truthy, execute it again and again. +Ցիկլը սկզբում կաշխատացնի մարմինը, ապա կստուգի պայմանը, և քանի դեռ այն ճշմարիտ է, այն կաշխատացնի մարմինը նորից ու նորից։ -For example: +Օրինակ․ ```js run let i = 0; @@ -80,109 +80,109 @@ do { } while (i < 3); ``` -This form of syntax should only be used when you want the body of the loop to execute **at least once** regardless of the condition being truthy. Usually, the other form is preferred: `while(…) {…}`. +Այս գրելաձևը պետք է օգտագործվի միայն այն դեպքում, երբ ցանկանում եք, որ ցիկլի մարմինը աշխատի **առնվազն մեկ անգամ** անկախ պայմանի ճշմարիտ կամ սխալական լինելու փաստից։ Սովորաբար մյուս տեսքը ավելի նախընտրելի է․ `while(…) {…}`։ -## The "for" loop +## "for" ցիկլը -The `for` loop is more complex, but it's also the most commonly used loop. +`for` ցիլկը ավելի բարդ է, բայց այն ամենից շատ կիրառվող ցիկլն է։ -It looks like this: +Այն ունի հետևյալ տեսքը․ ```js for (begin; condition; step) { - // ... loop body ... + // ... ցիկլի մարմին ... } ``` -Let's learn the meaning of these parts by example. The loop below runs `alert(i)` for `i` from `0` up to (but not including) `3`: +Եկեք ուսումնասիրենք այդ մասերը օրինակի վրա։ Ներքևի ցիկլը կատարում է `alert(i)` հրամանը `i`֊ն `0`֊ից մինչև `3`֊ը ոչ ներառյալ: ```js run -for (let i = 0; i < 3; i++) { // shows 0, then 1, then 2 +for (let i = 0; i < 3; i++) { // ցույց կտա 0, հետո 1, հետո 2 alert(i); } ``` -Let's examine the `for` statement part-by-part: +Դիտարկենք `for`֊ը մաս առ մաս․ -| part | | | +| մաս | | | |-------|----------|----------------------------------------------------------------------------| -| begin | `let i = 0` | Executes once upon entering the loop. | -| condition | `i < 3`| Checked before every loop iteration. If false, the loop stops. | -| body | `alert(i)`| Runs again and again while the condition is truthy. | -| step| `i++` | Executes after the body on each iteration. | +| սկիզբ | `i = 0` | Կատարվում է, երբ մտնում ենք ցիկլ։ | +| պայման | `i < 3`| Ստուգվում է ցիկլի ամեն իտերացիայից առաջ։ Եթե այն սխալ է, ապա ցիկլը կանգնեցվում է։ | +| մարմին | `alert(i)`| Աշխատում է այնքան ժամանակ, քանի դեռ պայմանը ճշմարիտ է։ | +| քայլ| `i++` | Կատարվում է մարմնի կատարումից հետո, ամեն իտերացիայի ժամանակ։ | -The general loop algorithm works like this: +Ընդհանուր դեպքում ալգորիթմը աշխատում է հետևյալ կերպ․ ``` -Run begin -→ (if condition → run body and run step) -→ (if condition → run body and run step) -→ (if condition → run body and run step) +Աշխատացնել *սկիզբ* +→ (եթե *պայման* → կատարել *մարմին* և աշխատացնել *քայլ*) +→ (եթե *պայման* → կատարել *մարմին* և աշխատացնել *քայլ*) +→ (եթե *պայման* → կատարել *մարմին* և աշխատացնել *քայլ*) → ... ``` -That is, `begin` executes once, and then it iterates: after each `condition` test, `body` and `step` are executed. +Այսինքն՝ `սկիզբ`֊ը կատարվում է մեկ անգամ, և հետո սկսվում են իտերացիաները․ ամեն `պայման`֊ի ստուգումից հետո, `մարմին`֊ը և `քայլ`֊ը կատարվում են։ -If you are new to loops, it could help to go back to the example and reproduce how it runs step-by-step on a piece of paper. +Եթե ցիկլերի թեման նոր է ձեր համար, ապա օգտակար կլինի հետ գնալ օրինակներին և քայլ առ քայլ գրառել դրանց կատարման ընթացքները թղթի վրա։ -Here's exactly what happens in our case: +Ահա թե ինչ է տեղի ունենում կոնկրետ մեր օրինակի դեպքում․ ```js // for (let i = 0; i < 3; i++) alert(i) -// run begin +// աշխատացնել սկիզբը let i = 0 -// if condition → run body and run step +// եթե պայման → կատարել մարմինը և աշխատացնել քայլը if (i < 3) { alert(i); i++ } -// if condition → run body and run step +// եթե պայման → կատարել մարմինը և աշխատացնել քայլը if (i < 3) { alert(i); i++ } -// if condition → run body and run step +// եթե պայման → կատարել մարմինը և աշխատացնել քայլը if (i < 3) { alert(i); i++ } -// ...finish, because now i == 3 +// ...ավարտ, քանի որ i == 3 ``` -````smart header="Inline variable declaration" -Here, the "counter" variable `i` is declared right in the loop. This is called an "inline" variable declaration. Such variables are visible only inside the loop. +````smart header="Փոփոխականի ներկառուցված հայտարարում" +Այստեղ "հաշվիչ" փոփոխական `i`֊ն հայտարարված է հենց ցիկլի վրա։ Այն կոչվում է "ներկառուցված" փոփոխականի հայտարարում։ Այսպիսի փոփոխականները տեսանելի են միայն ցիկլի ներսում։ ```js run for (*!*let*/!* i = 0; i < 3; i++) { alert(i); // 0, 1, 2 } -alert(i); // error, no such variable +alert(i); // սխալ է, չկա այդպիսի փոփոխական ``` -Instead of defining a variable, we could use an existing one: +Փոփոխական հայտարարելու փոխարեն մենք կարող ենք օգտագործել արդեն գոյություն ունեցողը։ ```js run let i = 0; -for (i = 0; i < 3; i++) { // use an existing variable +for (i = 0; i < 3; i++) { // օգտագործվում է գոյություն ունեցող փոփոխական alert(i); // 0, 1, 2 } -alert(i); // 3, visible, because declared outside of the loop +alert(i); // 3, տեսանելի է, քանի որ հայտարարված է ցիկլից դուրս ``` ```` -### Skipping parts +### Մասերի բաց թողում -Any part of `for` can be skipped. +`for` ցիկլի ամեն մաս կաող է բաց թողնվել։ -For example, we can omit `begin` if we don't need to do anything at the loop start. +Օրինակ, կարող ենք բաց թողնել `սկիզբ`֊ը, եթե ցիկլի սկզբում գործողություն չունենք անելու։ -Like here: +Ինչպես այստեղ․ ```js run -let i = 0; // we have i already declared and assigned +let i = 0; // i֊ն արդեն հայտարարված և արժեք ստացած է -for (; i < 3; i++) { // no need for "begin" +for (; i < 3; i++) { // կարիք չկա "սկիզբ"֊ի alert( i ); // 0, 1, 2 } ``` -We can also remove the `step` part: +Մենք կարող ենք նաև հեռացնել `քայլ`֊ի հատվածը․ ```js run let i = 0; @@ -192,32 +192,32 @@ for (; i < 3;) { } ``` -This makes the loop identical to `while (i < 3)`. +Այն դառնում է նույնաբար `while (i < 3)` ցիկլը։ -We can actually remove everything, creating an infinite loop: +Մենք կարող ենք հեռացնել ամեն բան, և կստանանք անվերջ ցիկլ․ ```js for (;;) { - // repeats without limits + // կրկնվում է անվերջ } ``` -Please note that the two `for` semicolons `;` must be present. Otherwise, there would be a syntax error. +Նկատի ունեցեք, որ `for`֊ի երկու կետ ստորակետները `;` պարտադիր պետք է գրված լինեն, հակառակ դեպքում տեղի կունենա սինտաքսի սխալ (syntax error)։ -## Breaking the loop +## Ցիկլի ընդհատում (break) -Normally, a loop exits when its condition becomes falsy. +Սովորական դեպքերում ցիկլը դուրս է գալիս (ավարտվում է) այն ժամանակ, երբ նրա պայմանը դառնում է սխալական։ -But we can force the exit at any time using the special `break` directive. +Բայց մենք կարող ենք հարկադրաբար (force) դուրս գալ ցիկլից, ամեն պահի, օգտագործելով հատուկ դիրեկտիվ `break`֊ը։ -For example, the loop below asks the user for a series of numbers, "breaking" when no number is entered: +Օրինակ՝ ներքևի ցիկլը օգտատիրոջից անընդհատ թվային մուտք է ուզում, "ընդհատվում" է, երբ ոչ թվային արժեք է մուտքագրվում․ ```js run let sum = 0; while (true) { - let value = +prompt("Enter a number", ''); + let value = +prompt("Մուտքագրեք թիվ", ''); *!* if (!value) break; // (*) @@ -226,35 +226,35 @@ while (true) { sum += value; } -alert( 'Sum: ' + sum ); +alert( 'Գումարը՝ ' + sum ); ``` -The `break` directive is activated at the line `(*)` if the user enters an empty line or cancels the input. It stops the loop immediately, passing control to the first line after the loop. Namely, `alert`. +`break` դիրեկտիվը կատարվում է `(*)` տողում, եթե օգտատերը մուտքագրում է դատարկ տող կամ չեղարկում է ներմուծման դաշտը։ Այն անմիջապես դադարեցնում է ցիկլը, և փոխանցում կատարումը ցիկլի անմիջապես հաջորդող տողին։ Մեր դեպքում՝ `alert`։ -The combination "infinite loop + `break` as needed" is great for situations when a loop's condition must be checked not in the beginning or end of the loop, but in the middle or even in several places of its body. +"Անվերջ ցիկլ + `break`" կոմբինացիան հիանալի լուծում է այն դեպքերի համար, երբ ցիկլի պայմանը պետք է ստուգվի ոչ թե սկզբում կամ վերջում, այլ ցիկլի մարմնի մեջ, նույնիսկ կարող է ստուգվել մի քանի տեղ։ -## Continue to the next iteration [#continue] +## Շարունակել հաջորդ իտերացիայից [#continue] -The `continue` directive is a "lighter version" of `break`. It doesn't stop the whole loop. Instead, it stops the current iteration and forces the loop to start a new one (if the condition allows). +`continue` դիրեկտիվը `break`֊ի ավելի "թեթև տարբերակն" է։ Այն չի կանգնեցնում ամբողջ ցիկլը, այլ կանգնեցնում է միայն ընթացիկ իտերացիան, և հարկադրում ցիկլին անցնել հաջորդին (եթե պայմանը ճշմարիտ է)։ -We can use it if we're done with the current iteration and would like to move on to the next one. +Մենք կարող ենք օգտագործել այն, եթե այլևս կարիք չկա շարունակել այդ պահի իտերացիան և ցանկանում ենք անցնել հաջորդին։ -The loop below uses `continue` to output only odd values: +Ներքևի ցիկլը օգտագործում է `continue`֊ն՝ ցուցադրելու միայն կենտ արժեքները․ ```js run no-beautify for (let i = 0; i < 10; i++) { - // if true, skip the remaining part of the body + // եթե ճիշտ է, բաց է թողնում ցիկլի մարմնի ներքևի հատվածը *!*if (i % 2 == 0) continue;*/!* - alert(i); // 1, then 3, 5, 7, 9 + alert(i); // 1, հետո 3, 5, 7, 9 } ``` -For even values of `i`, the `continue` directive stops executing the body and passes control to the next iteration of `for` (with the next number). So the `alert` is only called for odd values. +`i`֊ի զույգ արժեքների համար `continue` դիրեկտիվը կանգնեցնում է մարմնի կատարումը և կատարումը փոխանցում `for`֊ի հաջորդ իտերացիային (հաջորդ թվով)։ Այսպիսով, `alert`֊ը կանչվում է միայն կենտ արժեքների համար։ -````smart header="The `continue` directive helps decrease nesting" -A loop that shows odd values could look like this: +````smart header="`continue` դիրեկտիվը օգնում է քչացնել ներդրվածությունները" +Ցիկլը, որը ցույց է տալիս միայն կենտ արժեքները կարող էր ունենալ հետևյալ տեսքը․ ```js run for (let i = 0; i < 10; i++) { @@ -266,15 +266,15 @@ for (let i = 0; i < 10; i++) { } ``` -From a technical point of view, this is identical to the example above. Surely, we can just wrap the code in an `if` block instead of using `continue`. +Տեխնիկական տեսանկյունից վերևում բերված երկու օրինակները ամբողջովին նույնն են։ Իհարկե, մենք կարող ենք շրջապատել մեր կոդի այդ հատվածը `if` բլոկով, `continue`֊ի փոխարեն։ -But as a side-effect, this created one more level of nesting (the `alert` call inside the curly braces). If the code inside of `if` is longer than a few lines, that may decrease the overall readability. +Բայց մենք կստանանք լրացուցիչ ներդրված բլոկներ (`alert`֊ի կանչումը ձևավոր փակագծերի ներսում)։ Եթե `if`֊ի ներսի կոդը մի քանի տողից երկար է, ապա այն կարող է նվազեցնել ընդհանուր ծրագրի ընթեռնելիությունը։ ```` -````warn header="No `break/continue` to the right side of '?'" -Please note that syntax constructs that are not expressions cannot be used with the ternary operator `?`. In particular, directives such as `break/continue` aren't allowed there. +````warn header="Ոչ մի `break/continue` '?'֊ի աջ կողմում" +Ուշադրություն դարձրեք, որ սինտաքսիկ կոնդտրուկցիաները (syntax constructs) չեն հանդիսանում արտահայտություններ (expressions) և չեն կարող օգտագործվել `?` օպերատորի հետ։ Մասնավոր դեպքում, `break/continue` և նմանատիպ դիրեկտիվները թույլատրված չեն '?'֊ի հետ։ -For example, if we take this code: +Օրինակի համար, եթե դիտարկենք այս ծրագիրը․ ```js if (i > 5) { @@ -284,114 +284,114 @@ if (i > 5) { } ``` -...and rewrite it using a question mark: +...և ձևափոխենք այն օգտագործելով հարցականի նշանը․ ```js no-beautify -(i > 5) ? alert(i) : *!*continue*/!*; // continue isn't allowed here +(i > 5) ? alert(i) : *!*continue*/!*; // continue֊ն թույլատրված չէ այստեղ ``` -...it stops working: there's a syntax error. +...այն կդադարի աշխատել, քանի որ կա սինտաքսի սխալ։ -This is just another reason not to use the question mark operator `?` instead of `if`. +Սա մեկ այլ պատճառ է, որպեսզի չօգտագործենք `?`֊ը `if`֊ի փոխարեն։ ```` -## Labels for break/continue +## Պիտակներ (labels) break֊ի և continue֊ի համար -Sometimes we need to break out from multiple nested loops at once. +Կան դեպքեր, երբ մենք ցանկանում ենք դուրս գալ միանգամից մի քանի դերդրված ցիկլերից միանգամից։ -For example, in the code below we loop over `i` and `j`, prompting for the coordinates `(i, j)` from `(0,0)` to `(2,2)`: +Օրինակ, ներքևի ծրագրում մենք անցնում ենք ցիկլ `i` և `j` փոփոխականներով, `prompt`-ի օգնությամբ հարցնում `(i, j)` կոորդինատների համար արժեք, սկսած `(0,0)` մինչև `(2,2)`․ ```js run no-beautify for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { - let input = prompt(`Value at coords (${i},${j})`, ''); + let input = prompt(`Արժեքը (${i},${j}) կոորդինատում`, ''); - // what if we want to exit from here to Done (below)? + // ի՞նչ եթե ցանկանում ենք դուրս գալ այստեղից, և անցնել Պատրաստ է (ներքև) տողին } } -alert('Done!'); +alert('Պատրաստ է!'); ``` -We need a way to stop the process if the user cancels the input. +Մեզ պետք է ինչ֊որ ձև կանգնեցնել պրոցեսը, եթե օգտատերը չեղարկել է ներմուծման դաշտը։ -The ordinary `break` after `input` would only break the inner loop. That's not sufficient -- labels, come to the rescue! +Ստանդարտ `break`֊ը `input`֊ից հետո կկանգնեցնի միայն ներսի ցիկլը։ Դա բավարար չէ, և այստեղ է, որ պիտակները գալիս են օգնության։ -A *label* is an identifier with a colon before a loop: +*Պիտակը* դա նույնականացուցիչ (identifier) է վերջակետով ցիկլից առաջ․ ```js labelName: for (...) { ... } ``` -The `break ` statement in the loop below breaks out to the label: +`break ` հրամանը ներքևի ցիկլում կանգնեցնում է դրսի ցիկլը և անցնում պիտակի ցիկլի վերջը․ ```js run no-beautify *!*outer:*/!* for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { - let input = prompt(`Value at coords (${i},${j})`, ''); + let input = prompt(`Արժեքը (${i},${j}) կոորդինատում`, ''); - // if an empty string or canceled, then break out of both loops + // եթե դատարկ տող է կամ չեղարկված, ապա երկու ցիկլերն էլ կանգնեցվում են if (!input) *!*break outer*/!*; // (*) - // do something with the value... + // արժեքի հետ ինչ֊որ գործողություն... } } -alert('Done!'); +alert('Պատրաստ է!'); ``` -In the code above, `break outer` looks upwards for the label named `outer` and breaks out of that loop. +Վերևի ծրագրում `break outer`֊ը փնտրում է իրեն ընդգրկող դրսի ցիկլը, որի պիտակը `outer` է, և կանգնեցնում այն։ -So the control goes straight from `(*)` to `alert('Done!')`. +Այդպիսով կատարումը `(*)`֊ից անցնում է միանգամից `alert('Done!')`։ -We can also move the label onto a separate line: +Մենք նաև կարող ենք պիտակները տեղադրել ուրիշ տողում․ ```js no-beautify outer: for (let i = 0; i < 3; i++) { ... } ``` -The `continue` directive can also be used with a label. In this case, code execution jumps to the next iteration of the labeled loop. +`continue` դիրեկտիվը նույնպես կարող է կիրառվել պիտակների հետ։ Այս դեպքում, ծրագրի կատարումը անցնում է նշված պիտակով ցիկլի հաջորդ իտերացիային։ -````warn header="Labels do not allow to \"jump\" anywhere" -Labels do not allow us to jump into an arbitrary place in the code. +````warn header="Պիտակները թույլ չեն տալիս \"ցատկել\" կամայական տեղ" +Պիտակները թույլ չեն տալիս մեզ ցատկել ծրագրի լրիվ այլ հատված։ -For example, it is impossible to do this: +Օրինակ, անհնար է անել այսպիրի հնարք․ ```js -break label; // jump to the label below (doesn't work) +break label; // ցատկ ներքևի պիտակին (չի աշխատի) label: for (...) ``` -A `break` directive must be inside a code block. Technically, any labelled code block will do, e.g.: +`break` դիրեկտիվը պետք է լինի պիտակը պարունակող բլոկի ներսում։ Տեխնիկապես կամայական պիտակավորված բլոկի համար կարող ենք անել հետևյալը․ ```js label: { // ... - break label; // works + break label; // կաշխատի // ... } ``` -...Although, 99.9% of the time `break` is used inside loops, as we've seen in the examples above. +...Չնայած դրան, 99.9% դեպքերում `break`֊ը օգտագործվում է ցիկլերի ներսում, ինչպես տեսանք վերևի օրինակում։ -A `continue` is only possible from inside a loop. +`continue`֊ն միայն հնարավոր է կիրառել ցիկլի ներսում։ ```` -## Summary +## Ամփոփում -We covered 3 types of loops: +Մենք դիտարկեցինք ցիկլերի 3 տիպ․ -- `while` -- The condition is checked before each iteration. -- `do..while` -- The condition is checked after each iteration. -- `for (;;)` -- The condition is checked before each iteration, additional settings available. +- `while` -- Ստուգվում է պայմանը ամեն իտերացիայից առաջ։ +- `do..while` -- Ստուգվում է պայմանը ամեն իտերացիայից հետո։ +- `for (;;)` -- Ստուգվում է պայմանը ամեն իտերացիայից առաջ, և տալիս լրացուցիչ կարգավորումների հնարավորություն -To make an "infinite" loop, usually the `while(true)` construct is used. Such a loop, just like any other, can be stopped with the `break` directive. +"Անվերջ" ցիկլ ստանալու համար սովորաբար օգտագործում են `while(true)`։ Այս ցիկլը, ինչպես նաև կամայական այլ ցիկլ, կարող է կանգնեցվել `break` դիրեկտիվի միջոցով։ -If we don't want to do anything in the current iteration and would like to forward to the next one, we can use the `continue` directive. +Եթե մենք չենք ուզում, որ աշխատի ընթացիկ իտերացիան, այլ ցանկանում ենք անցնել հաջորդին, ապա կարող ենք օգտագործել `continue` դիրեկտիվը։ -`break/continue` support labels before the loop. A label is the only way for `break/continue` to escape a nested loop to go to an outer one. +`break/continue` կարող են կիրառվել ցիկլերից առաջ գրված պիտակների վրա։ Պիտակը միակ ձևն է `break/continue`֊ի միջոցով ներդրված ցիկլերից դրսի ցիկլը կանգնեցնելու կամ դրա հաջորդ իտերացիային անցնելու համար։