Significant figures - toPrecision() Not Working

Significant figures - toPrecision() Not Working

by Daniel Milan -
Number of replies: 9

Hello (again).  This is quickly become my favorite place to be throughout the day.

I was trying to use the Javascript toPrecision() method (https://moodleformulas.org/course/view.php?id=22&section=27) and I cannot for the life of me get it to work.  I even went so far as to copy and past all the exact html and script into a new question and it failed to work when transferred yet it works in the xml file that I downloaded and imported.

I have attached an xml file of my experimenting and I do not see why it will not work.

<p>{a}<br></p>
<p><span id="xr11"></span></p>
<script>
    document.getElementById("xr11").innerHTML = f201803241848({
        a
    }, 1);

    function f201803241848(x, s) {
        var xr = x.toPrecision(s);
        return xr;
    }
</script>

{a} is defined as 1.23456789 in the global variables.

Any help on what I am missing and how to make this work would be appreciated.

Thanks

Average of ratings: -
In reply to Daniel Milan

Re: Significant figures - toPrecision() Not Working

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Daniel,

You must trigger the function. This can be done with a button, an image that is loaded or, if you don't have any buttons or images, just the page that is loaded, with the "window.onload" event.

Here is the correct HTML code for the text of your question:

<p>{a}</p>
<p id="xr11"></p>
<script>
window.onload = function() {
    document.getElementById("xr11").innerHTML = myFunction({a}, 1);
    function myFunction(x, s) {
        var xr = x.toPrecision(s);
        return xr;
    }
}
</script>

I recommend w3schools for more information on javascript.

In reply to Dominique Bauer

Re: Significant figures - toPrecision() Not Working

by Daniel Milan -

Thank you again Dominique.  I would not have know that a trigger was needed.  

I have used w3school for help with HTML but haven't for much Javascript.  On the topic of w3school, I notice that they usually lead HTML in to additional CSS.  I have tried to use some of the CSS, but because my code language is so poor, I usually just copy and paste and try to make things work.  The CSS never seems to work.  Do you know if there is a way to use it along with the HTML?

I will definately have to spend a lot more time learning Java and HTML so as to limit the times I need to post here.  Quarantine life has me trying out a lot of new things in Moodle.  The learning curve has been quite steep these last few weeks.

Again, thank you.

Edit: Quick follow-up, I just tried the code you provided and it still doesn't work.  I left in the radio button click I had put into the original xml file and that still did not trigger it.  I believe we are still running Moodle 3.6 would that make a difference?

In reply to Daniel Milan

Re: Significant figures - toPrecision() Not Working

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Daniel,

You can put CSS inside <style></style> tags at the very top of the HTML code of a label or a question text. You must use the "Plain text area" editor because the "Atto HTML editor" erases the CSS. You must also set the format to "HTML format":




If you use the Atto editor, click on the HTML button and place the code in the question text:


You will get this:


where 1e+8 is 123456789 written with 1 significant digit.

In reply to Dominique Bauer

Re: Significant figures - toPrecision() Not Working

by Daniel Milan -
Thanks Dominique. I was entering it in the Atto editor and it was not working. For some reason though when I made the slightest change of

document.getElementById("xr11").innerHTML = myFunction({
a
}, 5);


to

document.getElementById("xr11").innerHTML = myFunction({a}, 5);

It started to work. I'm seriously confused by it, but it is working so I wont complain.

Thanks for the CSS information as well. I feel bad to have to ask for all this help but I don't know where else to look.

I really appreciate everything.

I made this video to show you what I mean exactly. I do not see why this should happen.
https://www.loom.com/share/46aa6c2d50b34006a080261005b655b1
In reply to Daniel Milan

Re: Significant figures - toPrecision() Not Working

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Daniel,

You have started to do something a little out of the ordinary. You should therefore use the Plain text area editor rather than the Atto editor.

"a" is a variable of the question Formulas. The {} curly braces are not javascript braces. They should stay right against a, that is, you should always write {a} without spaces.

Since the Atto editor thinks they are javascript braces, it has them in the usual way for ordinary code and nothing works anymore.

So from now on you will have to use the Plain text area editor all the time except to import images when you will have to use the Atto editor.

In reply to Dominique Bauer

Re: Significant figures - toPrecision() Not Working

by Daniel Milan -
Well I certain am falling down the rabbit hole faster and faster with this one. 

I did not even know that there was an option to change the editor preferences.  I was confused by you screen shot showing the option to use HTML.  I was not aware of the HTML format option and it had never come up before today.  I was further confused by "plain text area editor" in your most recent reply. 

Now however, after finding a video about changing editor preferences, that is all making sense.  I see I have a long, long way to go with all of this.  You have taught me so much in the last two days.  I cannot thank you enough.  

Out of curiosity, what is your preferred editor setting?  Do you work mainly in plain text or the atto HTML?

Thanks


In reply to Dominique Bauer

Re: Significant figures - toPrecision() Not Working

by Bernat Martinez -
Hello Dominique,
We have also faced this ATTO beautified code. It breaks formulas variables.
We have solved it installing old ATTO editor each time we make an upgrade. But it is not a perfect solution.
In reply to Bernat Martinez

Re: Significant figures - toPrecision() Not Working

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Daniel,

Out of curiosity, what is your preferred editor setting? Do you work mainly in plain text or the atto HTML?

Personally, I now use the Plain text area editor all the time, except when I have to import an image and must use the Atto editor. When the image is loaded, I return to the plain text area editor.

The Atto editor performs many checks and adjustments to the HTML syntax. It's very useful. For example, if you forget to include a closing tag such as </div>, the Atto editor will provide it for you. Sometimes the Atto editor is not practical, for example with Formulas variables and <style> tags.

The Plain text area editor does not check or adjust the code. Therefore, you can use Formulas variables without problem and you can insert <style> tags. However, if you forget to include a closing tag such as </div>, the Plain text area will not provide it.

Warning: If you use the Plain text area editor and forget to include a closing tag such as </div>, it will not be provided by the editor. If you mistakenly omit a </div> tag in a label, for example, this can really mess up your page and it can be difficult to fix it.
Average of ratings: Useful (1)
In reply to Bernat Martinez

Re: Significant figures - toPrecision() Not Working

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Bernat,

Good idea!
In my book, a "not perfect solution" is still a solution. smile