www.webdeveloper.com
Results 1 to 2 of 2

Thread: Better more efficient way to code?

  1. #1
    Join Date
    Jun 2014
    Posts
    6

    Better more efficient way to code?

    The scenario:
    A bricklayer wants an app on his phone that calculates quotes for brick retaining walls. Plan (pseudo code), research and code (including //comments where appropriate), test and evaluate).
    50 bricks per square metre
    Bricks cost $435 per 1000
    Brickie charges $1050 per $1000 to lay.


    I was wondering if their was a quicker better way to code this using (for loops, arrays) idk what you could use but this code I wrote looks ugly basic and primitive
    Thanks!


    PHP Code:
    (y being number of sq metres to lay)

    50 number of bricks needed (a)

    435 1000 cost per brick (b)

    cost of bricks (c)

    (
    1000) * 1050 labor cost (d)

    total cost

    of course what you want to 
    do with remaindersetcis something you should think about 


    2. 
    (y being height in metres of subject





    PHP Code:
    <!DOCTYPE html>
    <
    html>
    <
    head>
    <
    titleScenario 2 </title>
    </
    head>
    <
    body>
    <
    script>
    var 
    userLength  prompt("What is the length (m) of the area");
    var 
    userWidth prompt("What is the width (m) of the area")
    var 
    userLength  userWidth
    document
    .write("the total square metres of bricks are " y);
    var 
    bricksNeeded 50
    document
    .write("The total amount of brick needed is " bricksNeeded);
    var 
    costEachBrick 435/1000
    document
    .write("The cost of one brick is $" costEachBrick);
    var 
    totalCostBricks bricksNeeded*costEachBrick
    document
    .write("The total cost of all the bricks are $" totalCostBricks);
    var 
    laborCost = (totalCostBricks 1000 ) * 1050
    document
    .write("The cost to hire labor workers is $" laborCost);
    var 
    totalCost totalCostBricks laborCost
    document
    .write("Your total cost (including labor and bricks expense) is $" totalCost);
    var 
    charge 1050/1000
    document
    .write("The cost per brick for customer is $" +charge );
    var 
    totalCharge bricksNeeded charge
    document
    .write("The total to charge the customer is $" totalCharge );
    var 
    profit totalCharge totalCost
    document
    .write("the profit/loss you make from this sale is $" profit);
    </script>
    </body>
    </html> 

  2. #2
    Join Date
    Nov 2010
    Posts
    1,038
    both prompt() and document.write() are considered obsolete - the first is only really for debugging purposes these days and the second has one very obscure use that has nothing to do with what you are doing.

    Form elements are the preferred method for getting user input and either DOM methods or innerHTML are better ways of displaying dynamic content.

    At the very least I would put line breaks into my output.

    From what I can see, there is nothing really to loop over or put into an array. Sometimes code is cumbersome. If you were to continue with document.write you could get all the variables at the start and use one call to display everything...
    Code:
    <script> 
    var userLength  = prompt("What is the length (m) of the area"); 
    var userWidth = prompt("What is the width (m) of the area"); 
    
    var y = userLength  * userWidth; 
     
    var bricksNeeded = y * 50; 
    
    var costEachBrick = 435/1000; 
    
    var totalCostBricks = bricksNeeded*costEachBrick; 
    
    var laborCost = (totalCostBricks / 1000 ) * 1050; 
    
    var totalCost = totalCostBricks + laborCost; 
    
    var charge = 1050/1000; 
    
    var totalCharge = bricksNeeded * charge; 
    
    var profit = totalCharge - totalCost;
    
    document.write("the total square metres of bricks are " + y + "<br>"
    +"The total amount of brick needed is " + bricksNeeded+"<br>"
    +"The cost of one brick is $" + costEachBrick + "<br>"
    +"The total cost of all the bricks are $" + totalCostBricks + "<br>"
    +"The cost to hire labor workers is $" + laborCost + "<br>"
    +"Your total cost (including labor and bricks expense) is $" + totalCost + "<br>"
    +"The cost per brick for customer is $" +charge + "<br>"
    +"The total to charge the customer is $" + totalCharge + "<br>"
    +"the profit/loss you make from this sale is $" + profit);
    </script>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles