// write a program that prints two numbers, the numbers of cows and chickens on a farm, with the words Cows and Chickens after them, and zeros padded before both numbers so that athey are always three digits long.
// 007 Cows
// 011 Chickens
/*
function printFarmInventory(cows, chickens){
var cowString = String(cows);
while (cowString.length < 3){
cowString = "0" + cowString;
}
console.log(cowString + " Cows");
var chickenString = String(chickens);
while (chickenString.length < 3){
chickenString = "0" + chickenString;
}
console.log(chickenString + " Chickens");
}
printFarmInventory(7,11);
*/
// farmer says he wants to add pigs, before you copy and paste, there has to be a better way!
/*
// first attempt
function printZeroPaddedWithLabel(number, label){
var numberString = String(number);
while (numberString.length < 3){
numberString = "0" + numberString;
}
console.log(numberString + " " + label);
}
function printFarmInventory(cows, chickens, pigs){
printZeroPaddedWithLabel(cows, "Cows");
printZeroPaddedWithLabel(chickens, "Chickens");
printZeroPaddedWithLabel(pigs, "Pigs");
}
printFarmInventory(7,11,3);
*/
function zeroPad(number, width) {
var string = String(number);
while (string.length < width) {
string = "0" + string;
}
return string;
}
function printFarmInventory(cows, chickens, pigs) {
console.log(zeroPad(cows, 4) + " Cows");
console.log(zeroPad(chickens, 4) + " Chickens");
console.log(zeroPad(pigs, 4) + " Pigs");
}
printFarmInventory(7, 16, 3);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.