startit/eloquentjavascript/functions.js

90 lines
2.3 KiB
JavaScript
Raw Normal View History

2024-08-19 14:41:28 +02:00
const square = function (x) {
2024-08-19 13:36:00 +02:00
return x * x;
2024-08-19 14:41:28 +02:00
}
const hummus = function (factor) {
const ingredient = function (amount, unit, name) {
let ingredientAmount = amount * factor;
if (ingredientAmount > 1) {
unit += "s";
}
console.log(`${ingredientAmount} ${unit} ${name}`);
};
ingredient(1, "can", "chickpeas");
ingredient(0.25, "cup", "tahini");
ingredient(0.25, "cup", "lemon juice");
ingredient(1, "clove", "garlic");
ingredient(2, "tablespoon", "olive oil");
ingredient(0.5, "teaspoon", "cumin");
};
2024-08-21 11:01:49 +02:00
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);