this and functions
This commit is contained in:
parent
84430fe3ff
commit
cc7087a735
|
@ -19,3 +19,71 @@ const hummus = function (factor) {
|
|||
ingredient(0.5, "teaspoon", "cumin");
|
||||
};
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue