Advertisement
gitlez

Cookie Cartons

May 6th, 2011
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>Cookie Shipping</title>
  4. <script type="text/javascript" language="Javascript">
  5. /*
  6. A box of cookies can hold 24 cookies. A container can hold 75 boxes of cookies. Write a program that prompts the user to enter the total number of cookies, the number of cookies in a box, and the number of cookie boxes in a container. The program then outputs the number of boxes and containers necessary to ship the cookies. Note that each box must contain the specified number of cookies and each container must contain the specified number of boxes. If the last box of cookies contains less than the number of specified cookies, you can discard it, and output the number of leftover boxes.
  7. */
  8. function el(id){
  9. return document.getElementById(id);
  10. }
  11. function outputHTML(){
  12. var h = '<h3>Cookie Cartons Produced</h3>';
  13. h += 'Number of Cookies: ' + numCookies + '<br>';
  14. h += 'Number of Boxes: ' + numBoxes + '<br>';
  15. h += 'Number of Cartons: ' + numCartons + '<br>';
  16. return h;
  17. }
  18. function promptResponseNum(text, dText, required){ // A Prompt Response Which Must Be An Number
  19. var v = prompt(text + '?' + ((required)? ' *Required' : ''),dText);
  20. while(((v == '' || v == undefined || v == null ) && required) || parseFloat(v) != v){
  21. v = prompt('** Required Number Input **\n\n' + text + '?', dText);
  22. }
  23. return v;
  24. }
  25. var numCookies = promptResponseNum('Total Number of Cookies');
  26. var cpb = promptResponseNum('How many Cookies per Box','24');
  27. var bpc = promptResponseNum('How many Boxes Per Container','75');
  28. var numBoxes = Math.floor(numCookies/cpb);
  29. var numCartons = Math.floor(numBoxes/bpc);
  30. window.onload = function(){
  31. el('displayBox').innerHTML = outputHTML();
  32. }
  33. </script>
  34. </head>
  35. <body>
  36. <div id="displayBox">
  37.  
  38. </div>
  39. </body>
  40. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement