From cc7087a735969af2bdfe7069c442acaec488d0f5 Mon Sep 17 00:00:00 2001 From: Geir Okkenhaug Jerstad Date: Wed, 21 Aug 2024 11:01:49 +0200 Subject: [PATCH] this and functions --- eloquentjavascript/functions.js | 70 ++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/eloquentjavascript/functions.js b/eloquentjavascript/functions.js index ec78d69..5b842e3 100644 --- a/eloquentjavascript/functions.js +++ b/eloquentjavascript/functions.js @@ -18,4 +18,72 @@ const hummus = function (factor) { ingredient(2, "tablespoon", "olive oil"); ingredient(0.5, "teaspoon", "cumin"); }; -hummus(10); \ No newline at end of file +hummus(10); + +function power(base, exponent){ + if (exponent === 0) { + return 1; + } else { + return base * power(base, exponent -1); + } + +} +console.log(power(2, 3)); + +function findSolution(target){ + function find(current, history){ + if (current == target){ + return history; + } else if (current > target){ + return null; + } else { + return find(current + 5, `(${history} + 5)`) ?? + find (current * 3, `(${history} * 3)`); + } + } + return find(1, "1"); +} +console.log(findSolution(24)); + +function printFarmInventory(cows, chickens) { + let cowString = String(cows); + while (cowString.length < 3) { + cowString = "0" + cowString; + } + console.log(`${cowString} Cows`); + let chickenString = String(chickens); + while (chickenString.length < 3){ + chickenString = "0" + chickenString; + } + console.log(`${chickenString} Chickens`); +} +printFarmInventory(7, 11); + +function printZeroPaddedWithLabel(number, label) { + let numString = String(number); + while (numString.length < 3) { + numString = "0" + numString; + } + console.log(`${numString} ${label}`); +}; + +function printFarmInventory_2(cows, chickens, pigs) { + printZeroPaddedWithLabel(cows, "Cows"); + printZeroPaddedWithLabel(chickens, "Chickens"); + printZeroPaddedWithLabel(pigs, "Pigs"); +} +printFarmInventory_2(7, 11, 3); + +function zeroPad(number, width){ + let string = String(number); + while (string.length < width) { + string = "0" + string; + } + return string; +} +function printFarmInventory_3(cows, chickens, pigs) { + console.log(`${zeroPad(cows, 3)} Cows`); + console.log(`${zeroPad(chickens, 3)} Chickens`); + console.log(`${zeroPad(pigs, 3)} Pigs`); +} +printFarmInventory_3(7, 16, 3);