diff --git a/scripts/lab4.js b/scripts/lab4.js index ce452ba..1111d99 100644 --- a/scripts/lab4.js +++ b/scripts/lab4.js @@ -6,15 +6,17 @@ * @returns The sum of the two numbers if add is true and false otherwise. */ function sumValues(num1, num2, add) { - if (add) { - const result = 0; + if (add === true) { + if (typeof num1 !== 'number' || typeof num2 !== 'number') { + return false; + } result = num1 + num2; return result; } else { - return !add; + return false; } } @@ -25,15 +27,19 @@ function sumValues(num1, num2, add) { * @returns An array of each price's new price, after the discount is applied. Or false, if prices array is empty. */ function discountPrices(prices, discount) { + if (!Array.isArray(prices) || typeof discount !== 'number') { + return false; + } + if (prices.length === 0) return false; + const discounted = [] - const length = prices.length; let discountedPrice = 0 - for(let i = 0; i < length; i++) { - discountedPrice += prices[i] * (1 - discount); + for (let i = 0; i < prices.length; i++) { + let discountedPrice = prices[i] * (1 - discount); discounted.push(discountedPrice); } return discounted; } -module.exports = {sumValues, discountPrices}; \ No newline at end of file +module.exports = {sumValues, discountPrices};