SUGGESTION: It's probably best if you try to ask one question per thread. Questions with multiple facets are fine, but asking multiple questions, even seemingly related ones, is problematic for this format.
1: […] Why doesn't the second if resolve as true and displays its content if I go <<back>> and on the passage again?
Because the <<back>> macro explicitly undoes the moment, it rewinds the history—the state modifications you made within the passage are undone. If you want to return to the previous passage while retaining the history, you'll want to use either the <<return>> macro or some kind of link using the previous() function. For a few examples of the latter:
→ Links using the previous() function
[[Return|previous()]]
<<link "Return" `previous()`>><</link>>
<<button "Return" `previous()`>><</link>>
2: Then is <<set $Variable used across the entire game/passages?
When you set a story variable (variables starting with a dollar sign) it exists, with the value you set, from that moment until the end of the game, unless you either change its value to something else or unset it.
Temporary variables (variables starting with an underscore) exist from the moment they're set until the next turn/moment.
SEE: SugarCube's TwineScript document.
3: And can I set and change a $Variable in a function in Story Javascript that I <<run myFunction()>> in a passage?
Yes, using the State API—specifically, State.variables. Although, how you may do so depends on exactly what you're doing and how. The most significant hurdle/gotcha is scope. Your function needs to be within a scope where it can be accessed in later <<run>> or <<script>> macro invocations. The two foolproof ways to go about this is to either make your function an auto-global (or a method of an auto-global object) or a method of SugarCube's setup object.
Using the setup object is safer, as there's no chance of clobbering or shadowing a host object or other built-in, which is possible using auto-globals.
Example 1A: Auto-global function
/* Add your function as an auto-global. */
window.yourFunctionName = function () {
/* Set $qsolved to false. */
State.variables.qsolved = false;
};
Usage:
<<run yourFunctionName();>>
/* OR */
<<script>>yourFunctionName();<</script>>
Example 1B: Method of an auto-global object
/* Set up the Game object as an auto-global. */
window.Game = {};
/* Add your method to the Game object. */
Game.yourMethodName = function () {
/* Set $qsolved to false. */
State.variables.qsolved = false;
};
Usage:
<<run Game.yourMethodName();>>
/* OR */
<<script>>Game.yourMethodName();<</script>>
Example 2: Method of the setup object
/* Add your method to the setup object. */
setup.yourMethodName = function () {
/* Set $qsolved to false. */
State.variables.qsolved = false;
};
Usage:
<<run setup.yourMethodName();>>
/* OR */
<<script>>setup.yourMethodName();<</script>>