If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Need Help - Specific Javascript Converters

Hi all,

I'm new here *waves*. Basically, in a nutshell I have absolutely no experience with javascript. I know HTML and a enough CSS to get me by but learning javascript at almost 32 is sending me blind! :lol:

I'm looking for help with learning how to code my own specific javascript converter. As I said before, I'm completely new with this so bare with me if I'm end up going cross-eyed or getting confused.

I'd like to set up specific recipe converters on my website, Sweet 2 Eat Baking. By specific, I mean coded with certain answers. An example of this would be a oven temperature converter. On most, if I enter 180℃ and convert to ℉, it gives me 354℉. This is technically 'correct' but in reality, it's 350℉ which I'd like to be used as a specific answer. Does that make sense?

I'd like a temperature converter with my specific answers for input and output either way (from F to C and C to F). I also want cup measurements to metric and metric to cup and I have a few of these to implement due to different weights with flours, sugars, liquids, etc.

I'd like to set up specific recipe converters on my website, Sweet 2 Eat Baking. By specific, I mean coded with certain answers. An example of this would be a oven temperature converter. On most, if I enter 180℃ and convert to ℉, it gives me 354℉. This is technically 'correct' but in reality, it's 350℉ which I'd like to be used as a specific answer. Does that make sense?

Only partially. What do you mean by a "specific answer"? Do you mean a result rounded up/down/to the nearest 5 or 10 degrees?

I am not sufficiently knowledgeable about cooking to know what is meant by a "cup". Does it differ with the weight of the ingredient? The only cups I know much about are A, B, C and D. As I understand it, no matter what size cup is used, the ingredients of a recipe measured with the same size cup will have their volumes in the same proportion to one another.

The script suggested by sunfighter is very old-fashioned and uses the much-despised eval(). eval() is evil!!! Here is a basic converter which rounds the result to the nearest 5 degrees. Obviously you can easily change 5 to 10 or whatever for either/both conversions. You can round the result down (as opposed to round to the nearest) by using Math.floor rather than Math.round. Or Math.ceil if you want to round up.

If it does not meet your needs, feel free to come back. I will be most happy to bare with you at any time! Your place or mine?

Only partially. What do you mean by a "specific answer"? Do you mean a result rounded up/down/to the nearest 5 or 10 degrees?

Not as such. As stupid as it sounds, with baking the exact answers aren't what is used in cookbooks and such. So, when using an online converter including the one you provided below (and thank you for that) it gives an accurately correct answer but is technically incorrect because in cookbooks 180℃ = 350℉ and vice versa. When using the javascript code, it gives me an answer of 355℉ instead of 350℉.

So we'll call the calculated (javascript converted) answers as 'correct' and the cookbook answers as 'cookbook' in order for me to even attempt to explain more.

It's hard to explain but I'd like it to convert into the 'cookbook' answers instead of the 'correct'. Does that make sense?

I'd like the 'cookbook' answers above to be the ones that are displayed when entered. So, the example above of if someone enters 180℃ and gets an answer of 355℉ is not what I'm looking for, I'd like the answer to say 350℉.

If at all possible, I'd love if the Gas Marks (those people that have gas ovens) could be implemented too?

I hope I'm making some sort of sense? If not, enjoy my rambles!

Originally Posted by Philip M

I am not sufficiently knowledgeable about cooking to know what is meant by a "cup". Does it differ with the weight of the ingredient? The only cups I know much about are A, B, C and D. As I understand it, no matter what size cup is used, the ingredients of a recipe measured with the same size cup will have their volumes in the same proportion to one another.

As I'm sure you're aware, here in the UK when cooking, baking and such we weigh in grams (metric). In the US, they don't weigh at all, they use measuring cups (which can be bought here but not everyone knows that). The measuring cups come in certain measurements of 1/4 (60ml), 1/3 (80ml), 1/2 (125ml) and 1 cup (250ml).

There's a few problems. The main problem is that different ingredients have different weights (which I have already calculated and should be an easier conversion since the weight for 1 cup can be divided to give the correct cup measurement.)

Here's a few examples (I'm using UK names for ingredients so I don't confuse you further):

See the difference in weight between the different ingredients? To calculate between 1/4, 1/3 and 1/2, I just divided and then rounded up. Same with the Ounce (oz) values too.

Originally Posted by Philip M

The script suggested by sunfighter is very old-fashioned and uses the much-despised eval(). eval() is evil!!! Here is a basic converter which rounds the result to the nearest 5 degrees. Obviously you can easily change 5 to 10 or whatever for either/both conversions. You can round the result down (as opposed to round to the nearest) by using Math.floor rather than Math.round. Or Math.ceil if you want to round up.

Yup, I went cross-eyed there. But yes, modern is better, thank you.

Originally Posted by Philip M

If it does not meet your needs, feel free to come back. I will be most happy to bare with you at any time! Your place or mine?

If you want only these values, then a simple conversion table will do the job. But I don't feel like doing any more on this unless/until I am sure that I have fully understood what you want. You requirement, the whole requirement and nothing but the requirement, please. What about 210 and 250 degrees C? Do you want to convert only from Celcius to F or the reverse as well? Frankly if it is as simple as that I would just list the conversion table on the website.

I am not entirely sure I really understand the cups part.

What do you expect the user to enter as input and what output do you expect? Surely there are many different ingedients?
Personally, I would ignore the Americans. Let them eat cake. Or another idea is to have a separete website for US visitors with the receipes expressed in their (strange) measurements. I understand that they still use rods! (And perhaps roods - square rods).

Would it not be simpler to specify each of the ingredients in both metric and in "cups"? Rather than attempt dynamic conversion, which does not seem too practical to me if a cup of flour is 127g but a cup of butter is 226g and a cup of something else is 100g. I think I will stick with beans on toast - I do know how to cook that! On a good day I can boil an egg as well!

One possible approach is to set up a select list of every ingredient and when the user selects an item he gets the metric or imperial equivalent weight of one US cup of that item. Not sure how that helps. Surely the Americans must use peculiar fractions of a cup?

I may be a bit dim today, but I do not see how a UK recipe which calls for
200g plain flour
100g granulated sugar
75g cocoa powder
can be expressed in US cups, unless bizarre fractions are involved.

Last edited by Philip M; 02-24-2013 at 08:44 PM.

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

One problem with setting up converters for cooking measurements is that the conversions do not appear to be the same in all countries.

When I set up a converter for some cooking measures many years ago I set the tablespoon conversion to 20ml - which is what it is defined to be here - and got complaints from people in other countries that they couldn't use it on their sites because that conversion was wrong because the tablespoons in their country are a different size.

Not sure where the 180 C = 355 F came from since the correct answer is 356 - and it is always that regardless of where you are using it. So how big a tolerance is supposed to be allowed in cooking - 350 is 1.7% low. Based on the supplied list it looks like it is supposed to be rounded to the nearest 25 (and the conversion the other way is to the nearest 10 - which is a completely different tolerance since 10 C = 18 F).

If you want only these values, then a simple conversion table will do the job. But I don't feel like doing any more on this unless/until I am sure that I have fully understood what you want. You requirement, the whole requirement and nothing but the requirement, please. What about 210 and 250 degrees C? Do you want to convert only from Celcius to F or the reverse as well? Frankly if it is as simple as that I would just list the conversion table on the website.

I suppose for the temperatures, it probably would be easiest for the both of us to just have them listed like above. As for the 210 and 250, they're not listed in the books because they're not normally used in baking.

Originally Posted by Philip M

I am not entirely sure I really understand the cups part.

What do you expect the user to enter as input and what output do you expect? Surely there are many different ingedients?
Personally, I would ignore the Americans. Let them eat cake. Or another idea is to have a separete website for US visitors with the receipes expressed in their (strange) measurements. I understand that they still use rods! (And perhaps roods - square rods).

Forgive me for not knowing the technical terms but I'm mainly interested in the conversion between metric grams and US cups and perhaps a separate conversion area (separate javascript) for each ingredient and for it to calculate both ways (from grams to cups and cups to grams). I realise this would mean possibly coding 9 separate scripts - I'm clueless to this sort of thing. If it's too much work then I completely understand. Perhaps it might be easier (when I've attempted to explain everything) to hire for the work?

For example: picking granulated sugar as one of the ingredient examples...

I'd like a javascript converter which would go above and beyond the listed grams. So let's say someone wants to convert 500g of cocoa powder into cups, it's simple to see that 1 cup of cocoa powder is 100g so the end result would be 5 cups. And if possible vice versa too so a US user, using the same example, wants to know what the gram value of 5 cups is therefore listing 500g.

I could just go ahead and list all the ingredients and their conversions on my specific page created but I thought it would be more user-friendly and professional to have some sort of conversion tool.

Originally Posted by Philip M

Would it not be simpler to specify each of the ingredients in both metric and in "cups"? Rather than attempt dynamic conversion, which does not seem too practical to me if a cup of flour is 127g but a cup of butter is 226g and a cup of something else is 100g. I think I will stick with beans on toast - I do know how to cook that! On a good day I can boil an egg as well!

At the moment, I'm trying to do this but I use both UK and US recipes. Therefore I spend a lot of time physically weighing the ingredients to get the gram value when baking something. It's really time consuming and isn't 100% accurate. I'd hate for anyone to comment saying they tried my recipe and it failed because things weren't accurate in the first place.

Originally Posted by Philip M

One possible approach is to set up a select list of every ingredient and when the user selects an item he gets the metric or imperial equivalent weight of one US cup of that item. Not sure how that helps. Surely the Americans must use peculiar fractions of a cup?

Although I tried the above link entering cocoa powder into the ingredients (which I know is 100g to 1 cup) and entering 100g to cup but it seems to calculate at 1.17 cups and should be 1.00 (1) cup. *shrug*

Originally Posted by Philip M

I may be a bit dim today, but I do not see how a UK recipe which calls for
200g plain flour
100g granulated sugar
75g cocoa powder
can be expressed in US cups, unless bizarre fractions are involved.

The cups do come in fractions. You get 4 cups which are 1/4, 1/3, 1/2 and 1 cup. And you can create 2/3 and 3/4 out of them too.

But let me try. I'm not the best at maths so here goes...
200g plain flour - the closest I can to getting 200g is 3/4 of a cup (approx. 96g) I got this by multiplying 1/4 cup (32g) by 3.
100g granulated sugar - 1 cup of granulated sugar is 200g therefore halving that equals 100g = 1/2 cup
75g cocoa powder - 1 cup of cocoa powder is 100g. 1/4 of a cup equals 25g therefore 75g of cocoa powder would be 3/4 of a cup.

Again, if it's too much work or what I'm asking for isn't possible to achieve then it's fine. Thanks again for your time.

But if there a lot of diffferent ingredients and a lot of weights per ingredient, then it will be long job to code. If you are not able to do that yourself (and you say that you have absolutely no experience with javascript) then you will need to pay a professional to do the job. I just do not have the time! But good luck anyway.

Last edited by Philip M; 02-25-2013 at 09:39 AM.

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.