diff --git a/homeworks/andrii.morozov_AndriiMorozov88/GamesProject/calculator.html b/homeworks/andrii.morozov_AndriiMorozov88/GamesProject/calculator.html index 279c939a..e2daf204 100644 --- a/homeworks/andrii.morozov_AndriiMorozov88/GamesProject/calculator.html +++ b/homeworks/andrii.morozov_AndriiMorozov88/GamesProject/calculator.html @@ -58,6 +58,12 @@
diff --git a/homeworks/andrii.morozov_AndriiMorozov88/GamesProject/scripts/logic.js b/homeworks/andrii.morozov_AndriiMorozov88/GamesProject/scripts/logic.js index 603b216e..f71aca37 100644 --- a/homeworks/andrii.morozov_AndriiMorozov88/GamesProject/scripts/logic.js +++ b/homeworks/andrii.morozov_AndriiMorozov88/GamesProject/scripts/logic.js @@ -80,6 +80,5 @@ function showResult() { for (let count = 0; count < 10000000; count++); const timeFinish = getAbsoluteTime(); commentContainer.innerText = `Date of calculation: ${getDate()}. Time of function execution: ${timeFinish - timeStart} ms`; - console.log(getAbsoluteTime()); } -button.addEventListener('click', showResult); +button.addEventListener('click', showCards); diff --git a/homeworks/andrii.morozov_AndriiMorozov88/GamesProject/styles/calculator.css b/homeworks/andrii.morozov_AndriiMorozov88/GamesProject/styles/calculator.css index b5db3c67..eeb1cef8 100644 --- a/homeworks/andrii.morozov_AndriiMorozov88/GamesProject/styles/calculator.css +++ b/homeworks/andrii.morozov_AndriiMorozov88/GamesProject/styles/calculator.css @@ -70,4 +70,4 @@ font-size: 48px; font-weight: 600; color: #E35734; -} \ No newline at end of file +} diff --git a/homeworks/artur.kobyliatsky_Allocene/7-arrays-loops/arrays.js b/homeworks/artur.kobyliatsky_Allocene/7-arrays-loops/arrays.js new file mode 100644 index 00000000..26162aa1 --- /dev/null +++ b/homeworks/artur.kobyliatsky_Allocene/7-arrays-loops/arrays.js @@ -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"] diff --git a/homeworks/oleksandr.khodakivskyi_Moo0oony/7-array-loops/arrays.js b/homeworks/oleksandr.khodakivskyi_Moo0oony/7-array-loops/arrays.js new file mode 100644 index 00000000..cd5469f0 --- /dev/null +++ b/homeworks/oleksandr.khodakivskyi_Moo0oony/7-array-loops/arrays.js @@ -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)); diff --git a/homeworks/sasha.demidenko_SashaDemi/7-arrays_loops/arrays.js b/homeworks/sasha.demidenko_SashaDemi/7-arrays_loops/arrays.js new file mode 100644 index 00000000..db238a5d --- /dev/null +++ b/homeworks/sasha.demidenko_SashaDemi/7-arrays_loops/arrays.js @@ -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]; diff --git a/homeworks/sviatoslav.taranenko_SviatoslavTaranenko/7-arrays-loops/arrays.js b/homeworks/sviatoslav.taranenko_SviatoslavTaranenko/7-arrays-loops/arrays.js new file mode 100644 index 00000000..097da207 --- /dev/null +++ b/homeworks/sviatoslav.taranenko_SviatoslavTaranenko/7-arrays-loops/arrays.js @@ -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) { + // write your code here and instead of array return maxNumber + let maxNumber = array[0]; + for (let i = 1; i < array.length; i++) { + if (!Number.isNaN(array[i]) && 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 (!Number.isNaN(array[i]) && array[i] < minNumber) { + minNumber = array[i]; + } + } + return minNumber; + // write your code here and instead of array 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])) { + sum += array[i]; + } + } + return sum; + // write your code here and instead of array return sum of numbers +} + +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((El) => El < 0); + // write your code here and return array only with negative numbers +} + +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((El) => El > 0); + // write your code here and return array only positive numbers +} + +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]; + +function customReduce(array, reducer, initialValue) { + let accumulator = initialValue !== undefined ? initialValue : array[0]; + const startIndex = initialValue !== undefined ? 0 : 1; + + for (let i = startIndex; i < array.length; i++) { + accumulator = reducer(accumulator, array[i], i, array); + } + + return accumulator; +} + +function customMap(array, mapper) { + const result = []; + + for (let i = 0; i < array.length; i++) { + result.push(mapper(array[i], i, array)); + } + + return result; +} + +const numbers = [1, 2, 3, 4, 5]; + +const sum = customReduce(numbers, (acc, curr) => acc + curr, 0); +console.log(sum); + +const squaredNumbers = customMap(numbers, (num) => num ** 2); + +console.log(squaredNumbers); diff --git a/homeworks/vladyslav.brusentsov_vBrusentsov/7-array-loops/arrays.js b/homeworks/vladyslav.brusentsov_vBrusentsov/7-array-loops/arrays.js new file mode 100644 index 00000000..6ad45a69 --- /dev/null +++ b/homeworks/vladyslav.brusentsov_vBrusentsov/7-array-loops/arrays.js @@ -0,0 +1,128 @@ +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 (maxNumber < array[i]) { + 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 (minNumber > array[i]) { + 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 sumNumbers = 0; + for (let i = 0; i < array.length; i++) { + if (typeof array[i] === 'number' && !Number.isNaN(array[i])) { + sumNumbers += array[i]; + } + } + return sumNumbers; +} + +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 negativeNumbersArray = []; + for (let i = 0; i < array.length; i++) { + if (array[i] < 0) { + negativeNumbersArray.push(array[i]); + } + } + return negativeNumbersArray; +} + +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 positiveNumbersArray = []; + for (let i = 0; i < array.length; i++) { + if (array[i] > 0) { + positiveNumbersArray.push(array[i]); + } + } + return positiveNumbersArray; +} + +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]; + +function customMap(array, mapper) { + const result = []; + + for (let i = 0; i < array.length; i++) { + result.push(mapper(array[i], i, array)); + } + + return result; +} + +const firstDoubledNumbersArrays = customMap(arrayOne, (num) => num * 2); +console.log(firstDoubledNumbersArrays); +const secondDoubledNumbersArrays = customMap(arrayTwo, (num) => num * 2); +console.log(secondDoubledNumbersArrays); +const thirdDoubledNumbersArrays = customMap(arrayThree, (num) => num * 2); +console.log(thirdDoubledNumbersArrays); +const fourthDoubledNumbersArrays = customMap(arrayFour, (num) => num * 2); +console.log(fourthDoubledNumbersArrays); +const fifthDoubledNumbersArrays = customMap(arrayFive, (num) => num * 2); +console.log(fifthDoubledNumbersArrays); + +function customReduce(array, reducer, initialValue) { + let accumulator = initialValue !== undefined ? initialValue : array[0]; + const startIndex = initialValue !== undefined ? 0 : 1; + + for (let i = startIndex; i < array.length; i++) { + accumulator = reducer(accumulator, array[i], i, array); + } + + return accumulator; +} + +const firstNumbersSum = customReduce(arrayOne, (acc, curr) => acc + curr, 0); +console.log('Sum:', firstNumbersSum); +const secondNumbersSum = customReduce(arrayTwo, (acc, curr) => acc + curr, 0); +console.log('Sum:', secondNumbersSum); +const thirdNumbersSum = customReduce(arrayThree, (acc, curr) => acc + curr, 0); +console.log('Sum:', thirdNumbersSum); +const fourthNumbersSum = customReduce(arrayFour, (acc, curr) => acc + curr, 0); +console.log('Sum:', fourthNumbersSum); +const fifthNumbersSum = customReduce(arrayFive, (acc, curr) => acc + curr, 0); +console.log('Sum:', fifthNumbersSum); diff --git a/homeworks/yana.haivoronska_Yana_Haivoronska/5-logic/homework.js b/homeworks/yana.haivoronska_Yana_Haivoronska/5-logic/homework.js new file mode 100644 index 00000000..7b779cf6 --- /dev/null +++ b/homeworks/yana.haivoronska_Yana_Haivoronska/5-logic/homework.js @@ -0,0 +1,46 @@ +/** + * Perform a mathematical operation on two numbers. Return result or error. + * @param firstValue {string} with first number + * @param secondValue {string} with second number + * @param operation {string} with operation symbol + * @returns {string|number} result of calculation or error message: + * - if firstValue or secondValue is not a number, return 'Enter a number' + * - if operation is not '+', '-', '*', '/', return 'Choose a valid operation' + * - if the result is more than 100, return 'Result is too big' + * - else return number of result: 1 '+' 2 returns 3 + * + */ +function calculate(firstValue, secondValue, operation) { + const number1 = Number(firstValue); + const number2 = Number(secondValue); + + if (Number.isNaN(number1) || Number.isNaN(number2)) { + return 'Enter a number'; + } + if (firstValue === '' || secondValue === '') { + return 'Enter a number'; + } + let result; + switch (operation) { + case '+': + result = number1 + number2; + break; + case '-': + result = number1 - number2; + break; + case '*': + result = number1 * number2; + break; + case '/': + result = number1 / number2; + break; + default: + return 'Choose a valid operation'; + } + if (result > 100) { + return 'Result is too big'; + } + return result; +} + +window.calculate = calculate; diff --git a/homeworks/yana.haivoronska_Yana_Haivoronska/5-logic/index.html b/homeworks/yana.haivoronska_Yana_Haivoronska/5-logic/index.html new file mode 100644 index 00000000..0d128940 --- /dev/null +++ b/homeworks/yana.haivoronska_Yana_Haivoronska/5-logic/index.html @@ -0,0 +1,38 @@ + + +
+ +
+ + +
+