Click here to Skip to main content
15,039,149 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I'm new to programming and this is in JavaScript. I'm running it on Google and it doesn't run, it's straight out of the book. Can you tell me why and what I need to do? Thank you.
<pre lang="Javascript">

function zeroPad(number, width) {
    let string = String(number);
    while (string.length < width){
        string = "0" + string;
    }
    return string;
}

function printFarmInventory(cows, chickens, pigs) {
    console.log(`${zeroPad(cows, 3)} Cows`);
    console.log(`${zeroPad(chickens, 3)} Chickens`);
    console.log(`${zeroPad([pigs], 3)} Pigs`);
}

printFarmInventory(7, 16, 3);


What I have tried:

This is all i've tried bc this is what the book was showing.
Posted
Updated 17-Apr-20 14:51pm
Comments
[no name] 17-Apr-20 19:36pm
   
"and it doesn't run" is not very helpful. Please describe what you get and what you expect.
Patrice T 17-Apr-20 22:12pm
   
"it's straight out of the book."
because there is only 1 book.
Richard MacCutchan 18-Apr-20 3:37am
   
You already posted this in the Javascript forum. Please do not crosspost.
JaHowler 18-Apr-20 14:20pm
   
sorry, but it seems simple enough to read over and skip. Have a good one

1 solution

Is this what you were expecting? If so it is working perfectly fine
007 Cows
016 Chickens
003 Pigs
This is the code I ran, putting your code into an HTML file
HTML
<html>
<head>
<title>k</title>
</head>
<body>
<script type="text/javascript">
function zeroPad(number, width) {
    let string = String(number);
    while (string.length < width){
        string = "0" + string;
    }
    return string;
}

function printFarmInventory(cows, chickens, pigs) {
    console.log(`${zeroPad(cows, 3)} Cows`);
    console.log(`${zeroPad(chickens, 3)} Chickens`);
    console.log(`${zeroPad([pigs], 3)} Pigs`);
}

printFarmInventory(7, 16, 3);
</script>
</body>
</html>
So save this document, and open it up with your browser. You will get a blank page. That is fine.
What you need to do is to right-click and choose Inspect (or ctrl-shift-j) t open up the javascript console.
   
v2
Comments
JaHowler 18-Apr-20 14:21pm
   
Thank you, I appreciate your time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900