printFarmInventory(7,11,3);

by Shridhar Baddur

JavaScript

// 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);