MBCUK Posted November 25, 2013 Share Posted November 25, 2013 Hi there, I am trying to learn how to make a calculation script. I first started out trying to ask people online on javascript forums but for some reason no-one is very keen on helping me. I am not a coder or in work I am 16 and trying to create this for my ICT coursework, I am now afriad ive bitten off more than I can chew.. Basically, I have done the research as far as my forumulae is comcerend but I am having difficulties putting it all together onto a website. Please dont shoot me if this isnt correct but this is my forumlae example Math.round(400*(49+Math.max(count-35;0)*0.85)/count)/400 this is the problem im trying to solve: £49 for the first 35 items £0.85 each item thereafteruser inputs total amount of items eg 400 items400 items - 35 = 365365 * 0.85 = 310.25310.25 + 49 = 359.25Total Price £359.25Then the total price £359.25 / 400 to give the price per item £0.89 (to two decimal places) I really nope someone can help me out here or its back to the drawing bord Quote Link to comment https://forums.phpfreaks.com/topic/284264-php-and-math-calculations/ Share on other sites More sharing options...
Irate Posted November 25, 2013 Share Posted November 25, 2013 (edited) You made a syntax error at Math.max(count-35;0) (bolded the mistake for you). Besides, checking the maximum out of any value and 0 is basically pointless unless you know for sure that you're going to use negative values... Oh, use Number.toFixed(d) where d is the amount of decimal spots you want to have (the function may round up or down if you're near to an integer value). Edit: looks like I forgot that the ic code prevents BBCode from being parsed. Hah. Also, all of this is pretty basic work which could easily be found in Google. Edited November 25, 2013 by Irate Quote Link to comment https://forums.phpfreaks.com/topic/284264-php-and-math-calculations/#findComment-1460019 Share on other sites More sharing options...
MBCUK Posted November 25, 2013 Author Share Posted November 25, 2013 thankyou. should I google how to do this using php or stick to javascript. I have to change the numbers into varibles with an input form Quote Link to comment https://forums.phpfreaks.com/topic/284264-php-and-math-calculations/#findComment-1460024 Share on other sites More sharing options...
Irate Posted November 25, 2013 Share Posted November 25, 2013 You're validating forms? Use the server-side solution (you could also use server-side JS for that, though). php.net has all of the Math functions documented. Do not look any further than that if that's all you need. Quote Link to comment https://forums.phpfreaks.com/topic/284264-php-and-math-calculations/#findComment-1460027 Share on other sites More sharing options...
MBCUK Posted November 25, 2013 Author Share Posted November 25, 2013 I think for I might be aiming a bit high as far as my courwork is concerned, but thanks I really do appreciate the adivce I will look at php.net at least ive got 4 months to finish my website Quote Link to comment https://forums.phpfreaks.com/topic/284264-php-and-math-calculations/#findComment-1460029 Share on other sites More sharing options...
ignace Posted November 25, 2013 Share Posted November 25, 2013 (edited) function calculatePrice(totalItems) { if (totalItems < 1) { return 0; } if (totalItems <= 35) { return 49; } // we have more then 35 items // the first 35 go for 49 pound // all others (totalItems - 35) go for 0.85 return 49 + (totalItems - 35) * 0.85; } function calculatePricePerUnit(totalItems) { return (calculatePrice(totalItems) / totalItems).toFixed(2); } calculatePrice(35); // 49 calculatePrice(36); // 49.85 calculatePrice(400); // 359.25 calculatePricePerUnit(400); // 0.90EDIT: apparently I misinterpreted Edited November 25, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/284264-php-and-math-calculations/#findComment-1460031 Share on other sites More sharing options...
MBCUK Posted November 25, 2013 Author Share Posted November 25, 2013 ok.. I can understand this better! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/284264-php-and-math-calculations/#findComment-1460034 Share on other sites More sharing options...
MBCUK Posted November 25, 2013 Author Share Posted November 25, 2013 (edited) function calculatePrice(totalItems) { if (totalItems < 1) { return 0; } if (totalItems <= 35) { return 49; } // we have more then 35 items // the first 35 go for 49 pound // all others (totalItems - 35) go for 0.85 return 49 + (totalItems - 35) * 0.85; } calculatePrice(35); // 49 calculatePrice(36); // 49.85 calculatePrice(400); // 359.25EDIT: apparently I misinterpreted Sorry can I ask, how do I assign varibles so I can create a form for someone to input their own number of items... Edited November 25, 2013 by MBCUK Quote Link to comment https://forums.phpfreaks.com/topic/284264-php-and-math-calculations/#findComment-1460038 Share on other sites More sharing options...
ignace Posted November 25, 2013 Share Posted November 25, 2013 (edited) One way you could do it is, you create a new function that fills out the other fields in your form: function updatePricesOnClick(form) { form.pricePerUnit.value = calculatePricePerUnit(form.totalItems.value); form.price.value = calculatePrice(form.totalItems.value); }Your form would then look like: <form action="#"> <input type="text" name="totalItems" id="totalItems"> <input type="text" name="pricePerUnit" id="pricePerUnit"> <input type="text" name="price" id="price"> <input type="submit" onclick="updatePricesOnClick(this.parentElement); return false;"> </form>When you fill out the first field and press submit the other fields will be filled in. When you write code in the future make sure you separate the code that does calculations (or other app-related heavy-lifting like querying a database) and the code that displays things on the screen. Developers call this "separation of concern". EDIT: de-VB'ed it. Edited November 25, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/284264-php-and-math-calculations/#findComment-1460046 Share on other sites More sharing options...
MBCUK Posted November 25, 2013 Author Share Posted November 25, 2013 <script> function calculatePrice(totalItems) { if (totalItems < 1) { return 0; } if (totalItems <= 35) { return 49; } // we have more then 35 items // the first 35 go for 49 pound // all others (totalItems - 35) go for 0.85 return 49 + (totalItems - 35) * 0.85; } function updatePricesOnClick(form) { form.pricePerUnit.value = calculatePricePerUnit(form.totalItems.value); form.price.value = calculatePrice(form.totalItems.value); } </script> <form action="#"> <input type="text" name="totalItems" id="totalItems"> <input type="text" name="pricePerUnit" id="pricePerUnit"> <input type="text" name="price" id="price"> <input type="submit" onclick="updatePricesOnClick(this.parentElement); return false;"> </form> Is this correct? Quote Link to comment https://forums.phpfreaks.com/topic/284264-php-and-math-calculations/#findComment-1460050 Share on other sites More sharing options...
Solution ignace Posted November 25, 2013 Solution Share Posted November 25, 2013 (edited) No. I had already updated my code above. Here's a demo: http://jsfiddle.net/MKVR3/1/ Edited November 25, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/284264-php-and-math-calculations/#findComment-1460052 Share on other sites More sharing options...
MBCUK Posted November 25, 2013 Author Share Posted November 25, 2013 THANKYOU SO MUCH!!!! Quote Link to comment https://forums.phpfreaks.com/topic/284264-php-and-math-calculations/#findComment-1460054 Share on other sites More sharing options...
ignace Posted November 26, 2013 Share Posted November 26, 2013 (edited) if (totalItems <= 35) { return 49; }is wrong and should probably be: if (totalItems <= 35) { return (totalItems / 35) * 49; }but i don't know the exact requirements, so don't simply copy/paste this and think about how it is written and correct where necessary. It is not because someone on the internet gave you a working example that is the be all and end all of it. People make mistake, those on the internet do it even more. Edited November 26, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/284264-php-and-math-calculations/#findComment-1460118 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.