Copy link to clipboard
Copied
...
Hello !...
I'm working on the volumes and try to make an automatic calculator in javascript !
The problem is that I can't transform my french number with "," in a US format with "."...
So far (after researches on the net) I try :
1./
var _L_A_Cube_US = _L_A_Cube.toLocaleString("en-US");
var _V_Cube_US = (_L_A_Cube_US * _L_A_Cube_US * _L_A_Cube_US);
var _V_Cube = _V_Cube_US.toLocaleString("fr-FR");
2./
_L_A_Cube_US = _L_A_Cube.replace(' , ' , ' . ');
var _V_Cube_US = (_L_A_Cube_US * _L_A_Cube_US * _L_A_Cube_US);
var _V_Cube = _V_Cube_US.toLocaleString("fr-FR");
3./
_L_A_Cube_US = new Intl.NumberFormat("en-US").format(_L_A_Cube);
var _V_Cube_US = (_L_A_Cube_US * _L_A_Cube_US * _L_A_Cube_US);
var _V_Cube = _V_Cube_US.toLocaleString("fr-FR");
But none of them work as I want...
So, some javascript help is needed !...
Thanks in advance !...
😉
...
The problem is that you are trying to multiply strings, not numbers. That is what toLocaleString does.
I can't really see how this is all supposed to work, if 5,5 is a product or entered. But you could make that a US number format by doing this,
var num = "5,5";
var _num = Number(num.replace(",", "."));
I played around a bit with this using the concepts from my High-Low Game.
Give this a shot.
I used the variables of side and vol - just change them as needed.
Hope this helps.
// You could place this onEnter
function stripComma() {
side=side.toString();
side=side.split("");
for (i=0;i<side.length;++i) {
if (side[i]==",") {
side[i]=".";
--i;
}
}
}
// You can place this code on your button to calculate the volume
stripComma();
side=side.joi
...
Copy link to clipboard
Copied
...
I found a solution !... (The example number 2 of my previous post in fact !...)
I just have to correct the first line with deleting a space !...
_L_A_Cube_US = _L_A_Cube.replace(' , ', ' . ');
😉
...
Copy link to clipboard
Copied
...
Pffff...
It's seems to be not as simple...
If the number is not decimal, the replace function doesn't work (normal !) but returns nothing...
Still searching so...
😉
...
Copy link to clipboard
Copied
The problem is that you are trying to multiply strings, not numbers. That is what toLocaleString does.
I can't really see how this is all supposed to work, if 5,5 is a product or entered. But you could make that a US number format by doing this,
var num = "5,5";
var _num = Number(num.replace(",", "."));
Copy link to clipboard
Copied
...
Thanks for your answer !...
5,5 is entered by the learners...
After searching all day, even in french javascript forums, I didn't find an easy and simple solution !...
Javascript is not made by/for the french !...
And if I changed the variables in US format, I will have a lot of variables (the picture attached is just one of many slides)...
So I decided to go for the simpliest thing, I asked my learners to replace the "," by a "." if they entered a decimal !!!...
Thanks for your solution anyway ! (no doubt it will be useful)
😉
...
Copy link to clipboard
Copied
...
Ultimately, I try your solution !...
As you can see the TEB registers a variable named : _L_A_Cube
And the learner could enter whatever they want...
So the script to calculate the volume of the cube is :
var _L_A_Cube_US = Number(_L_A_Cube.replace(",", "."));
var _V_Cube_US = (_L_A_Cube_US * _L_A_Cube_US * _L_A_Cube_US);
var _V_Cube = _V_Cube_US.toLocaleString("fr-FR");
This is ok with a decimal number... But if I enter an integer it doesn't work anymore ????!...
(That's what I tried to explain in my previous post, just before your answer...)
😉
...
Copy link to clipboard
Copied
You'll need to check if it is has a comma first:
var _L_A_Cube_US;
if ( _L_A_Cube.indexOf(",") !== -1 )
{
_L_A_Cube_US = Number(_L_A_Cube.replace(",", "."));
}
else
{
_L_A_Cube_US = Number(_L_A_Cube);
}
var _V_Cube_US = (_L_A_Cube_US * _L_A_Cube_US * _L_A_Cube_US);
var _V_Cube = _V_Cube_US.toLocaleString("fr-FR");
Copy link to clipboard
Copied
...
I thought of this solution yesterday but was unable to write it by myself !...
This line was impossible to find and write for me :
if ( _L_A_Cube.indexOf(",") !== -1 )
I will test this right away !
Big thank you once again !
😉
...
Copy link to clipboard
Copied
...
Still a problem !...
The code is nice for decimal but still doesn't work for integer...
So I try to reverse the if and else :
var _L_A_Cube_US;
if ( _L_A_Cube.indexOf(",") === -1 )
{
_L_A_Cube_US = Number(_L_A_Cube);
}
else
{
_L_A_Cube_US = Number(_L_A_Cube.replace(",", "."));
}
var _V_Cube_US = (_L_A_Cube_US * _L_A_Cube_US * _L_A_Cube_US);
var _V_Cube = _V_Cube_US.toLocaleString("fr-FR");
It's still working nicely with decimal... But not with integer (returns nothing !)...
So this line must have a problem :
_L_A_Cube_US = Number(_L_A_Cube);
???
😉
...
Copy link to clipboard
Copied
I have no idea, it is working both ways for me in Captivate.
you could log each variable to see where the issue is.
console.log(_L_A_Cube.indexOf(","), _L_A_Cube_US, _V_Cube_US);
press F12 in the browser to see what is logged in the console tab.
Copy link to clipboard
Copied
...
So strange...
It works perfectly with decimal...
But with integer, I got an "undefined" response...
I also try with :
_L_A_Cube_US = window.cpAPIInterface.getVariableValue("_L_A_Cube");
But I'm starting to travel in unknown territories !...
Big thanks for your time !... Even if it's not successful, I've already learned many things thanks of you !
😉
...
Copy link to clipboard
Copied
Hmm...
I am reminded of the High-Low card game where we ran into a similar problem.
You may recall the formatting of the numbers issue we worked through.
In the end - I stripped out the comma, converted to a number (multiply by 1), then - once I got the number I desired after performing calculations, used toLocaleString to get you back to a proper format.
One of the issues we ran into was that formatting the space in the numbers required stripping the NARROW space versus a normal space.
Once I realized my comma stripping was working fine, I was able to zero in on the NARROW space.
Any chance of that going on?
Copy link to clipboard
Copied
I played around a bit with this using the concepts from my High-Low Game.
Give this a shot.
I used the variables of side and vol - just change them as needed.
Hope this helps.
// You could place this onEnter
function stripComma() {
side=side.toString();
side=side.split("");
for (i=0;i<side.length;++i) {
if (side[i]==",") {
side[i]=".";
--i;
}
}
}
// You can place this code on your button to calculate the volume
stripComma();
side=side.join("");
side=side*1;
vol = side*side*side;
vol = vol.toLocaleString(undefined);
side = side.toLocaleString(undefined);
Copy link to clipboard
Copied
...
Thanks Greg !...
It seems you find a solution !... And your code works as expected !...
(Too bad I can't really understand it !...)
...
But I understand quite easily the code TLCMedia gave me...
So do you have an idea why it doesn't really work for me ?...
...
It's time to close my eyes for today !...
Be back tomorrow !...
...
Big thank you to both of you !...
😉
...
Copy link to clipboard
Copied
I ran into the same issue at first with using an integer.
That is why I changed the integer to a string before trying to split.
I chose to split to an array, check for commas and change them to a decimal using a loop.
Then I reassemble the string and convert it to a number by multiplying by 1.
Then I perform the math and change it back to you local setting.
Copy link to clipboard
Copied
...
The cube was the easiest... (only 1 variable...)
Tomorrow I will have to test your code with the other formulas !...
I will come back to tell you how I manage it (or not !...)
Take care and happy Captivating !...
😉
...
Copy link to clipboard
Copied
...
Perfect !...
I just try to adapt your code on the cylinder volume (only 2 variables), and it was easy !...
So I presume I can do the same for the other slides !...
Thanks once again !...
🙂
...
Edit : Easier than I thought !
https://soutien67.fr/math/activites/volumes/Volumes_Th_03/index.html
Big thanks again to both of you !...
...