Skip to content
Open
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions js_hw2/html2.html
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>
11 changes: 11 additions & 0 deletions js_hw2/js1.js
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++ ) {

Copy link
Copy Markdown
Contributor Author

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

distance = 1.1 * distance;
totalDistance = totalDistance + distance;
}

totalDistance=totalDistance.toFixed ( 2 );

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces before and after = and redundant spaces before and after 2

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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")
17 changes: 17 additions & 0 deletions js_hw2/js10.js
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++;
}
15 changes: 15 additions & 0 deletions js_hw2/js11.js
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 ] );
}
11 changes: 11 additions & 0 deletions js_hw2/js2.js
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" );

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use strong equality === instead of ==

console.log ( dividerOfnaturalNumber );
}
11 changes: 11 additions & 0 deletions js_hw2/js3.js
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 ) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 ) );
}
12 changes: 12 additions & 0 deletions js_hw2/js4.js
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 ) ;
16 changes: 16 additions & 0 deletions js_hw2/js5.js
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 ) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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++ ) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 )
}
20 changes: 20 additions & 0 deletions js_hw2/js6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict"

let number = prompt ( 'Please enter a number' ) ;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when you get value as prompt ( 'Please enter a number' ) ;
you get number as string type, you can add + to make variable number as type 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 + ","));
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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

10 changes: 10 additions & 0 deletions js_hw2/js7.js
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 );
21 changes: 21 additions & 0 deletions js_hw2/js8.js
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")
}

16 changes: 16 additions & 0 deletions js_hw2/js9.js
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 ) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 );