On Using Javascript with persistent front and back numbers in conjunction with Mathjax

I’ve tried to make sense of these two pages:
yingtongli dot me’s website
SimonLammer’s anki-persistence github page

The first one I can’t get anything to work. The second one, I just don’t understand it. I wanted to make some math problem with changing input and use mathjax to display the math symbols properly. I can’t figure it out for the life of me. Any example decks that can help me understand would be greatly appreciated.

Instead of the second (anki-persistence), you can do the following:
Create a new card object property on the front in a script and assign a value to it. This value will be passed to the back of the card.

Front:

<script>
  card.number = 6 // assign any value you need
</script>

Back:

<script>
  var myNumber = card.number // 6 will be passed to this variable
</script>

This will work on Anki Desktop, AnkiWeb and AnkiMobile, but not on AnkiDroid, which rebuilds the document with each card (i.e. the <body> element you assign the property to is deleted).

Please keep in mind that using the const keyword outside of function scopes will cause an error (except on AnkiDroid) and can prevent your scripts from executing. You can safely use var here instead.

2 Likes

Is it possible I could look at a deck with a few examples? I will build upon it to produce a complete curriculum once I understand it and give it back to the community.

Thanks for your time everyone, it is greatly appreciated.

I don’t understand your directions, I apologize. How do I do what you are suggesting? What is “create a new card object property”? My google skills and anki knowledge seem bad in this realm. I apologize and I appreciate any help.

This is not Anki related, it’s a JS (JavaScript) thing. An object, in OOP (to which JS adheres, to some extent), is, poorly speaking, anything. Any value is an object. In this case, the object @bkztei is referring to is card. A property of an object is a variable bound to that object, which you can access with the property accessor operator, ie the dot: object.property. That sentence is just a way to say: if you have variables that “start with” card., you will be able to pass them around, with technical terms.

However, this is not cross platform. For a solution that works everywhere, you should really use anki-persistence instead.

Maybe something simpler to do would be if you could post one example of a math problem with all the possible inputs / outputs, and we can provide the correct code to make it work as you want it to.

Thanks for your time. If I could get an example of how to do this with persistent numbers on the front and back without having to create a new card for each problem - would be awesome. I’ll take it and run with it. I want to create the formula in a field and then set up the mathjax calling the variables in front and back.

x = \frac{-b ± \sqrt{b^2 -4ac}}{2a}

Anyone give me a hand with this example? I think it’ll be all I need to start creating a whole deck of good stuff to share. I just can quite get it it yet. Thanks.