I am trying to write a program that can do math with English words.
For example, I want to be able to do something like
"four thousand and three" + "seven thousand and twenty nine"
and get output like
"eleven thousand and thirty two"
Is it possible to do this in jQuery?
Usually when you encounter $() , that means the developer is using a javascript library, such as jQuery. The $ symbol is the namespace for those libraries. All the functions they define begin with $. , such as $. get() .
$ is another, which is just an alias to jQuery . $$ is not provided by jQuery. It's provided by other libraries, such as Mootools or Prototype.
$ and $$ are valid variable names in JavaScript, they have no special meaning. Usually they set their value to library instances, in your example if you check the closure call, at the end of the file you'll see that $ is jQuery in this case if it is defined and $$ is cytoscape.
An asterisk ( * ) is used to represent the multiplication operator.
Yes, I have written a jQuery plug-in called Word Math that was made for this exact purpose.
For the example in your question, you can just copy and paste this code
alert($.wordMath("four thousand and three").add("seven thousand and twenty nine"));
//alerts "eleven thousand thirty two"
and voila! You've performed some word math.
Word Math can also do conversion from Javascript numbers to words and vice versa:
$.wordMath.toString(65401.90332)
// sixty five thousand four hundred one and nine tenths and three thousandths and three ten thousandths and two hundred thousandths
$.wordMath("three million four hundred and sixty seven thousand five hundred and forty two").value
// 3467542
You can read more about how to use the Word Math plugin on its readme page
EDIT: There is now a version of Word Math that does not depend on jQuery. To use it, you should download the wordMath.vanilla.min.js
file on the gitHub repository instead of the wordMath.jquery.js
file.
The usage of the jQuery-less version is exactly the same as the jQuery version, except that you do not need the $.
prefix in the calls. In other words, instead of doing
$.wordMath("fifteen").add("eighteen")
you would instead write
wordMath("fifteen").add("eighteen")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With