-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 7-strings_dates_AndriiMorozov88
- Loading branch information
Showing
13 changed files
with
726 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,4 +70,4 @@ | |
font-size: 48px; | ||
font-weight: 600; | ||
color: #E35734; | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
homeworks/artur.kobyliatsky_Allocene/7-arrays-loops/arrays.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
const arrayOne = [3, 0, -5, 1, 44, -12, 3, 0, 0, 1, 2, -3, -3, 2, 1, 4, -2, -3, -1]; | ||
const arrayTwo = [-1, -8, -2]; | ||
const arrayThree = [1, 7, 3]; | ||
const arrayFour = [1, undefined, 3, 5, -3]; | ||
const arrayFive = [1, NaN, 3, 5, -3]; | ||
|
||
function maxNumberOfArr(array) { | ||
let maxNum = array[0]; | ||
for (let i = 0; i < array.length; i++) { | ||
const num = array[i]; | ||
if (num > maxNum) { | ||
maxNum = num; | ||
} | ||
} | ||
|
||
return maxNum; | ||
} | ||
|
||
console.log(maxNumberOfArr(arrayOne)); // 44 | ||
console.log(maxNumberOfArr(arrayTwo)); // -1 | ||
console.log(maxNumberOfArr(arrayThree)); // 7 | ||
console.log(maxNumberOfArr(arrayFour)); // 5 | ||
console.log(maxNumberOfArr(arrayFive)); // 5 | ||
|
||
function minNumberOfArr(array) { | ||
let minNum = array[0]; | ||
for (let i = 0; i < array.length; i++) { | ||
const num = array[i]; | ||
if (num < minNum) { | ||
minNum = num; | ||
} | ||
} | ||
|
||
return minNum; | ||
} | ||
|
||
console.log(minNumberOfArr(arrayOne)); // -12 | ||
console.log(minNumberOfArr(arrayTwo)); // -8 | ||
console.log(minNumberOfArr(arrayThree)); // 1 | ||
console.log(minNumberOfArr(arrayFour)); // -3 | ||
console.log(minNumberOfArr(arrayFive)); // -3 | ||
|
||
function sumNumbersOfArr(array) { | ||
let sum = 0; | ||
for (let i = 0; i < array.length; i++) { | ||
if (typeof array[i] === 'number' && !Number.isNaN(array[i])) { | ||
sum += array[i]; | ||
} | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
console.log(sumNumbersOfArr(arrayOne)); // 32 | ||
console.log(sumNumbersOfArr(arrayTwo)); // -11 | ||
console.log(sumNumbersOfArr(arrayThree)); // 11 | ||
console.log(sumNumbersOfArr(arrayFour)); // 6 | ||
console.log(sumNumbersOfArr(arrayFive)); // 6 | ||
|
||
function onlyNegativeNumbers(array) { | ||
const negativeNum = array.filter((num) => num < 0); | ||
return negativeNum; | ||
} | ||
|
||
console.log(onlyNegativeNumbers(arrayOne)); // [-5, -12, -3, -3, -2, -3, -1] | ||
console.log(onlyNegativeNumbers(arrayTwo)); // [-1, -8, -2] | ||
console.log(onlyNegativeNumbers(arrayThree)); // [] | ||
console.log(onlyNegativeNumbers(arrayFour)); // [-3] | ||
console.log(onlyNegativeNumbers(arrayFive)); // [-3] | ||
|
||
function onlyPositiveNumbers(array) { | ||
const positiveNum = array.filter((num) => num > 0); | ||
return positiveNum; | ||
} | ||
|
||
console.log(onlyPositiveNumbers(arrayOne));// [3, 1, 44, 3, 1, 2, 2, 1, 4] | ||
console.log(onlyPositiveNumbers(arrayTwo)); // []; | ||
console.log(onlyPositiveNumbers(arrayThree)); // [1, 7, 3]; | ||
console.log(onlyPositiveNumbers(arrayFour)); // [1, 3, 5]; | ||
console.log(onlyPositiveNumbers(arrayFive)); // [1, 3, 5]; | ||
|
||
// customReduce | ||
function customReduce(array, reducer, initialValue) { | ||
const isInitialValue = initialValue !== undefined; | ||
const startIndex = isInitialValue ? 0 : 1; | ||
let accumulator = isInitialValue ? initialValue : array[0]; | ||
|
||
for (let i = startIndex; i < array.length; i++) { | ||
accumulator = reducer(accumulator, array[i]); | ||
} | ||
|
||
return accumulator; | ||
} | ||
|
||
const numbers = [1, 2, 3, 4, 5]; | ||
|
||
const sum = customReduce(numbers, (accumulator, currentValue) => accumulator + currentValue, 0); | ||
|
||
console.log(sum); // result will be = 15 | ||
|
||
// customMap | ||
function customMap(array, mapper) { | ||
const mappedArray = []; | ||
for (let i = 0; i < array.length; i++) { | ||
mappedArray[i] = mapper(array[i]); | ||
} | ||
return mappedArray; | ||
} | ||
|
||
const objects = [{ name: 'John', age: 30 }, { name: 'Mary', age: 25 }]; | ||
|
||
const names = customMap(objects, (object) => object.name); | ||
|
||
console.log(names); // result will be = ["John", "Mary"] |
109 changes: 109 additions & 0 deletions
109
homeworks/oleksandr.khodakivskyi_Moo0oony/7-array-loops/arrays.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
const arrayOne = [3, 0, -5, 1, 44, -12, 3, 0, 0, 1, 2, -3, -3, 2, 1, 4, -2, -3, -1]; | ||
const arrayTwo = [-1, -8, -2]; | ||
const arrayThree = [1, 7, 3]; | ||
const arrayFour = [1, undefined, 3, 5, -3]; | ||
const arrayFive = [1, NaN, 3, 5, -3]; | ||
|
||
function maxNumberOfArr(array) { | ||
let maxNumber = array[0]; | ||
for (let i = 0; i < array.length; i++) { | ||
if (array[i] > maxNumber) { | ||
maxNumber = array[i]; | ||
} | ||
} | ||
return maxNumber; | ||
} | ||
|
||
console.log(maxNumberOfArr(arrayOne)); // 44 | ||
console.log(maxNumberOfArr(arrayTwo)); // -1 | ||
console.log(maxNumberOfArr(arrayThree)); // 7 | ||
console.log(maxNumberOfArr(arrayFour)); // 5 | ||
console.log(maxNumberOfArr(arrayFive)); // 5 | ||
|
||
function minNumberOfArr(array) { | ||
let minNumber = array[0]; | ||
for (let i = 0; i < array.length; i++) { | ||
if (array[i] < minNumber) { | ||
minNumber = array[i]; | ||
} | ||
} | ||
return minNumber; | ||
} | ||
|
||
console.log(minNumberOfArr(arrayOne)); // -12 | ||
console.log(minNumberOfArr(arrayTwo)); // -8 | ||
console.log(minNumberOfArr(arrayThree)); // 1 | ||
console.log(minNumberOfArr(arrayFour)); // -3 | ||
console.log(minNumberOfArr(arrayFive)); // -3 | ||
|
||
function sumNumbersOfArr(array) { | ||
let sum = 0; | ||
for (let i = 0; i < array.length; i++) { | ||
if (!Number.isNaN(array[i]) && array[i] !== undefined) { | ||
sum = array[i] + sum; | ||
} | ||
} | ||
return sum; | ||
} | ||
|
||
console.log(sumNumbersOfArr(arrayOne)); // 32 | ||
console.log(sumNumbersOfArr(arrayTwo)); // -11 | ||
console.log(sumNumbersOfArr(arrayThree)); // 11 | ||
console.log(sumNumbersOfArr(arrayFour)); // 6 | ||
console.log(sumNumbersOfArr(arrayFive)); // 6 | ||
|
||
function onlyNegativeNumbers(array) { | ||
const negativeArray = array.filter((number) => number < 0); | ||
return negativeArray; | ||
} | ||
|
||
console.log(onlyNegativeNumbers(arrayOne)); // [-5, -12, -3, -3, -2, -3, -1] | ||
console.log(onlyNegativeNumbers(arrayTwo)); // [-1, -8, -2] | ||
console.log(onlyNegativeNumbers(arrayThree)); // [] | ||
console.log(onlyNegativeNumbers(arrayFour)); // [-3] | ||
console.log(onlyNegativeNumbers(arrayFive)); // [-3] | ||
|
||
function onlyPositiveNumbers(array) { | ||
const positiveArray = array.filter((number) => number > 0); | ||
return positiveArray; | ||
} | ||
|
||
console.log(onlyPositiveNumbers(arrayOne));// [3, 1, 44, 3, 1, 2, 2, 1, 4] | ||
console.log(onlyPositiveNumbers(arrayTwo)); // []; | ||
console.log(onlyPositiveNumbers(arrayThree)); // [1, 7, 3]; | ||
console.log(onlyPositiveNumbers(arrayFour)); // [1, 3, 5]; | ||
console.log(onlyPositiveNumbers(arrayFive)); // [1, 3, 5]; | ||
|
||
// Additional tasks | ||
// Custom .map method | ||
|
||
function customMap(array, chosenFunction) { | ||
const editedArray = []; | ||
for (let i = 0; i < array.length; i++) { | ||
editedArray[i] = chosenFunction(array[i], i, array); | ||
} | ||
|
||
return editedArray; | ||
} | ||
|
||
const exampleFunction = (x) => x * 2; | ||
|
||
console.log(customMap(arrayOne, exampleFunction)); | ||
|
||
// Custom .reduce method | ||
|
||
function customReduce(array, chosenFunction, initialValue) { | ||
let accumulator = initialValue !== undefined ? initialValue : array[0]; | ||
const startingIndex = initialValue !== undefined ? 0 : 1; | ||
for (let i = startingIndex; i < array.length; i++) { | ||
accumulator = chosenFunction(accumulator, array[i], i, array); | ||
} | ||
|
||
return accumulator; | ||
} | ||
|
||
const chosenFunction = (x, y) => x + y; | ||
|
||
console.log(customReduce(arrayThree, chosenFunction, 0)); | ||
console.log('Custom reduce: ', customReduce(arrayThree, chosenFunction)); | ||
console.log('Native reduce: ', arrayThree.reduce(chosenFunction)); |
74 changes: 74 additions & 0 deletions
74
homeworks/sasha.demidenko_SashaDemi/7-arrays_loops/arrays.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const arrayOne = [3, 0, -5, 1, 44, -12, 3, 0, 0, 1, 2, -3, -3, 2, 1, 4, -2, -3, -1]; | ||
const arrayTwo = [-1, -8, -2]; | ||
const arrayThree = [1, 7, 3]; | ||
const arrayFour = [1, undefined, 3, 5, -3]; | ||
const arrayFive = [1, NaN, 3, 5, -3]; | ||
|
||
function maxNumberOfArr(array) { | ||
let maxNumber = array[0]; | ||
for (let i = 1; i < array.length; i++) { | ||
if (array[i] > maxNumber) { | ||
maxNumber = array[i]; | ||
} | ||
} | ||
return maxNumber; | ||
} | ||
|
||
console.log(maxNumberOfArr(arrayOne)); // 44 | ||
console.log(maxNumberOfArr(arrayTwo)); // -1 | ||
console.log(maxNumberOfArr(arrayThree)); // 7 | ||
console.log(maxNumberOfArr(arrayFour)); // 5 | ||
console.log(maxNumberOfArr(arrayFive)); // 5 | ||
|
||
function minNumberOfArr(array) { | ||
let minNumber = array[0]; | ||
for (let i = 1; i < array.length; i++) { | ||
if (array[i] < minNumber) { | ||
minNumber = array[i]; | ||
} | ||
} | ||
return minNumber; | ||
} | ||
|
||
console.log(minNumberOfArr(arrayOne)); // -12 | ||
console.log(minNumberOfArr(arrayTwo)); // -8 | ||
console.log(minNumberOfArr(arrayThree)); // 1 | ||
console.log(minNumberOfArr(arrayFour)); // -3 | ||
console.log(minNumberOfArr(arrayFive)); // -3 | ||
|
||
function sumNumbersOfArr(array) { | ||
let sum = 0; | ||
for (let i = 0; i < array.length; i++) { | ||
if (typeof array[i] !== 'number' || Number.isNaN(array[i])) { | ||
i += 1; | ||
} | ||
sum += array[i]; | ||
} | ||
return sum; | ||
} | ||
|
||
console.log(sumNumbersOfArr(arrayOne)); // 32 | ||
console.log(sumNumbersOfArr(arrayTwo)); // -11 | ||
console.log(sumNumbersOfArr(arrayThree)); // 11 | ||
console.log(sumNumbersOfArr(arrayFour)); // 6 | ||
console.log(sumNumbersOfArr(arrayFive)); // 6 | ||
|
||
function onlyNegativeNumbers(array) { | ||
return array.filter((e) => e < 0); | ||
} | ||
|
||
console.log(onlyNegativeNumbers(arrayOne)); // [-5, -12, -3, -3, -2, -3, -1] | ||
console.log(onlyNegativeNumbers(arrayTwo)); // [-1, -8, -2] | ||
console.log(onlyNegativeNumbers(arrayThree)); // [] | ||
console.log(onlyNegativeNumbers(arrayFour)); // [-3] | ||
console.log(onlyNegativeNumbers(arrayFive)); // [-3] | ||
|
||
function onlyPositiveNumbers(array) { | ||
return array.filter((e) => e > 0); | ||
} | ||
|
||
console.log(onlyPositiveNumbers(arrayOne));// [3, 1, 44, 3, 1, 2, 2, 1, 4] | ||
console.log(onlyPositiveNumbers(arrayTwo)); // []; | ||
console.log(onlyPositiveNumbers(arrayThree)); // [1, 7, 3]; | ||
console.log(onlyPositiveNumbers(arrayFour)); // [1, 3, 5]; | ||
console.log(onlyPositiveNumbers(arrayFive)); // [1, 3, 5]; |
Oops, something went wrong.