Repository navigation
add new homework. js2 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <title> Розв'язування задач за допомогою Java Script </title> | ||
| </head> | ||
| <body> | ||
| <script src=js1.js> | ||
| </script> | ||
| <script src=js2.js> | ||
| </script> | ||
| <script src=js3.js> | ||
| </script> | ||
| <script src=js4.js> | ||
| </script> | ||
| <script src=js5.js> | ||
| </script> | ||
| <script src=js6.js> | ||
| </script> | ||
| <script src=js7.js> | ||
| </script> | ||
| <script src=js8.js> | ||
| </script> | ||
| <script src=js9.js> | ||
| </script> | ||
| <script src=js10.js> | ||
| </script> | ||
| <script src=js11.js> | ||
| </script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| "use strict" | ||
|
|
||
| let distance = 10; | ||
| let totalDistance = distance; | ||
| for ( let day = 1; day < 7; day++ ) { | ||
| distance = 1.1 * distance; | ||
| totalDistance = totalDistance + distance; | ||
| } | ||
|
|
||
| totalDistance=totalDistance.toFixed ( 2 ); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spaces before and after = and redundant spaces before and after 2
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .toFixed is unreliable. For example 6.35.toFixed(1) == 6.3. You can ceate custom function which can use Math.round under hood. |
||
| console.log (totalDistance + " km") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| "use strict" | ||
|
|
||
| let a = 0, b = 0; | ||
| let heightOfTree = prompt ( "... and our tree has height:" ); | ||
|
|
||
| while ( a < heightOfTree ) { | ||
| let space = ""; | ||
| let branch = ""; | ||
| for ( b = 0; b < heightOfTree - a; b++ ) { | ||
| space += " "; | ||
| } | ||
| for ( b = 0; b < 2 * a + 1; b++ ) { | ||
| branch += "^"; | ||
| } | ||
| console.log ( space + branch ); | ||
| a++; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| "use strict" | ||
|
|
||
| let multiplicationTable = []; | ||
| for ( let i = 1; i <= 10; i++ ) | ||
| { | ||
| multiplicationTable [ i ] = []; | ||
| for ( let j = 1; j <= 10; j++ ) { | ||
| multiplicationTable [ i ] [ j ] = i * j; | ||
| } | ||
| /* if (multiplicationTable [ i ] [ j ] < 10 ) { | ||
| multiplicationTable [ i ] [ j ] = "0" + multiplicationTable [ i ] [ j ]; | ||
| console.log(multiplicationTable [ i ] ); | ||
| } else не працює */ | ||
| console.log(multiplicationTable [ i ] ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| "use strict" | ||
|
|
||
| let naturalNumber = prompt ( 'Please enter a natural number' ) ; | ||
| let dividerOfnaturalNumber; | ||
| if ( naturalNumber == 0) { | ||
| console.log ( "0" ); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use 0 instead of "0" for console.log. What difference between 0 and '0'? |
||
| } | ||
| for ( let dividerOfnaturalNumber = 1; dividerOfnaturalNumber <= naturalNumber; dividerOfnaturalNumber++ ) { | ||
| ( naturalNumber % dividerOfnaturalNumber == 0 ) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use strong equality === instead of == |
||
| console.log ( dividerOfnaturalNumber ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| "use strict" | ||
|
|
||
| let numberOfFactorial = prompt ( 'Please enter a natural number' ) ; | ||
| if (numberOfFactorial == 0) { | ||
| console.log ("0! = 1") | ||
| } else if (numberOfFactorial !== 0) { | ||
| function factorial ( numberOfFactorial ) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move factorial out of conditional statement |
||
| return ( numberOfFactorial != 1 ) ? numberOfFactorial * factorial(numberOfFactorial - 1 ) : 1; | ||
| } | ||
| console.log ( numberOfFactorial + "! = " + factorial ( numberOfFactorial ) ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| "use strict" | ||
|
|
||
| let pointAofDiapason = 37; | ||
| let pointBofDiapason = 63; | ||
| let sumOfOddNumbers = 0; | ||
|
|
||
| for ( let floatingPointOfDiapason = pointAofDiapason; floatingPointOfDiapason <= pointBofDiapason; floatingPointOfDiapason++ ) { | ||
| if (floatingPointOfDiapason % 2 == 1) { | ||
| sumOfOddNumbers += floatingPointOfDiapason; | ||
| } | ||
| } | ||
| console.log ( sumOfOddNumbers ) ; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| "use strict" | ||
|
|
||
| let numberForPrime = prompt ( 'Please enter a number' ); | ||
| if ( numberForPrime == 0 ) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use strong equality |
||
| console.log (0); | ||
| } else if ( numberForPrime == 1 ) { | ||
| console.log (1); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0 and 1 are not Simple number, you must not output this number |
||
| } | ||
| nextPrime: | ||
| for ( let numbersLessOfThis = 2; numbersLessOfThis < numberForPrime; numbersLessOfThis++ ) { | ||
| for ( let numbersLessOfThis2 = 2; numbersLessOfThis2 < numbersLessOfThis; numbersLessOfThis2++ ) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let numbersLessOfThis2, numbersLessOfThis - for variables for iterations in loops you may use i, j as the name of variables |
||
| if ( numbersLessOfThis % numbersLessOfThis2 == 0 ) | ||
| continue nextPrime; | ||
| } | ||
| console.log ( numbersLessOfThis ) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| "use strict" | ||
|
|
||
| let number = prompt ( 'Please enter a number' ) ; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when you get value as prompt ( 'Please enter a number' ) ; |
||
| let divider; | ||
| if ( number == 0) { | ||
| console.log ( "0" ); | ||
| } else if ( number > 0 ) { | ||
| for ( let divider = 1; divider <= number; divider++ ) { | ||
| ( number % divider == 0 ); | ||
| console.log ( divider + ","); | ||
| console.log ( -divider + ","); | ||
| } | ||
| } else if ( number < 0 ) { | ||
| for ( let divider = -1; divider >= number; divider-- ) { | ||
| ( number % divider == 0 ); | ||
| console.log ( divider + ","); | ||
| console.log ( Math.abs ( divider + ",")); | ||
| } | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use also equality Math.round(a/b) === a/b if you want to check this |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| "use strict" | ||
|
|
||
| let sumOfRow1 = 0; | ||
| let maxNumberOfRow1 = 100; | ||
| for (let numberOfRow1 = 1; numberOfRow1 <= maxNumberOfRow1; numberOfRow1++) { | ||
| sumOfRow1 = sumOfRow1 + 1 / numberOfRow1; | ||
| } | ||
| sumOfRow1 = sumOfRow1.toFixed (2); | ||
|
|
||
| console.log ( sumOfRow1 ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| "use strict" | ||
|
|
||
| let numberOfRow2 = 2; | ||
| let sumOfRow2 = 2; | ||
| while ( numberOfRow2 <= 10 ) { | ||
| function factorial ( numberOfRow2 ) { | ||
| return numberOfRow2 ? numberOfRow2 * factorial ( numberOfRow2 - 1 ) : 1; | ||
| } | ||
| sumOfRow2 = sumOfRow2 + ( 1 / factorial ( numberOfRow2 ) ); | ||
| numberOfRow2++ | ||
| } | ||
| console.log ( sumOfRow2 ); | ||
|
|
||
| if ( sumOfRow2 > Math.E ) { | ||
| console.log( "Sum of the row > e"); | ||
| } else if ( sumOfRow2 < Math.E ) { | ||
| console.log( "Sum of the row < e"); | ||
| } else if ( sumOfRow2 = Math.E ) { | ||
| console.log( "Sum of the row = e") | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| "use strict" | ||
|
|
||
| let sumOfRow3 = 0; | ||
| let valueOfRow3; | ||
| let numberOfRow3=2; | ||
|
|
||
| while ( true ) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is wrong using of while loop. Instead of true try to use condition 1/numberOfRow < 0.001 |
||
| valueOfRow3 = ( (-1) ** ( numberOfRow3 - 1 ) / numberOfRow3 ); | ||
| if (Math.abs ( valueOfRow3 ) < 0.001 ) { | ||
| break | ||
| } | ||
| sumOfRow3 += valueOfRow3; | ||
| numberOfRow3 += 1; | ||
| } | ||
| numberOfRow3 = numberOfRow3.toFixed ( 3 ); | ||
| console.log ( sumOfRow3 ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use 2 spaces instead of a tab