Variable error in loop

Hi so in the following block of code I am being returned a value I should not be, specifically the variable $j. It is returning $j as 1 instead of 3 which I want it too. What is the problem?

// START CODE BLOCK
That’s correct! Now let’s assume we want to know the reliability of the process at five given times (1, 5, 10, 30, 60 minutes). First input the MTTF you calculated in the last step, and then calculate the reliability at each time t (minutes).

CHECKPOINT START

MTTF: <<textbox “$mttf” “”>>

<<set $i=3>>
<<link “CHECK MY OBSERVATIONS”>><<replace “#test”>>
<<if 20 <= $mttf && $mttf <= 60>>
<<if $i == 3>>
<<set $scores = $scores + 1.0>>
<>
<<set $scores = $scores + 0>>
<>

<<set $r1 = Math.exp((1/$mttf)(1))>>
<<set $r5 = Math.exp((1/$mttf)
(5))>>
<<set $r10 = Math.exp((1/$mttf)(10))>>
<<set $r30 = Math.exp((1/$mttf)
(30))>>
<<set $r60 = Math.exp((1/$mttf)*(60))>>

Great! Now fill in the corresponding reliability for each given time:

Time t (minutes):

t = 1 minute:
Reliability = <<textbox “$response1” “”>>

t = 5 minutes:
Reliability = <<textbox “$response2” “”>>

t = 10 minutes:
Reliability = <<textbox “$response3” “”>>

t = 30 minutes:
Reliability = <<textbox “$response4” “”>>

t = 60 minutes:
Reliability = <<textbox “$response5” “”>>

<<if $visited == 1>>
<<set $j=3>>
<>
You have $j chances total. // THIS LINE RETURNS J = 1

<<if $j eq 0>>
[[CORRECT|Chapter64.2]]
<>

/* **** set up the limitations *** */
<<if $j gte 0>>
<<set $visited = 0>>

<<set $j = $j-1>>
<<button “Submit”>>

<<replace “#choice”>><<print "Your answer: ">>
$response1

<<if $response1 gte ($r1 - 0.05) and $response1 lte ($r1 + 0.05) and $response2 gte ($r5 - 0.05) and $response2 lte ($r5 + 0.05) and $response3 gte ($10 - 0.05) and $response3 lte ($r10 + 0.05) and $response4 gte ($r30 - 0.05) and $response4 lte ($r30 + 0.05) and $response5 gte ($r60 - 0.05) and $response5 lte ($r60 + 0.05)>>

/**** Add the score here **** */
<<set $chance = $j>>
<<if $j eq 2>>
<<set $scores = $scores + 5>>
<<elseif $j eq 1>>
<<set $scores = $scores + 4>>
<<elseif $j eq 0>>
<<set $scores = $scores + 3>>
<>
\

[[CORRECT|Chapter64.2]]
<>
[[INCORRECT|Chapter63.2.1]]
<>
<>
<>

<>
<>
<<popover ‘noclick’>>Sorry, please try again.<>
<>
<<set $i = $i - 1>>
<><>
// END CODE BLOCK

Hi there, could you put you code in a code block (CTRL+E or the </> option in the editing)? Because we are missing most of your code here to be able to help :slight_smile:
Thank you!

Sorry what do you mean put it in a block? This is one chapter of several, only the code in this block that i provided has teh error. Something is wrong with it such that it is returning j = 3 instead of j = 1.

SugarCube code disappears during posting unless you put it in a code block. Example:

<<if $var is true>> This is it <>

should show up as

<<if $var is true>> This is it <</if>>

To create it, you need to wrap your code with three backticks ( ` ), like this:
image

Oh ok I see. Like this? I removed some of the angle brackets so that you can see the code statements.

‘’’
That’s correct! Now let’s assume we want to know the reliability of the process at five given times (1, 5, 10, 30, 60 minutes). First input the MTTF you calculated in the last step, and then calculate the reliability at each time t (minutes).

CHECKPOINT START

MTTF: <<textbox “$mttf” “”>>

<<set $i=3>>
<<link “CHECK MY OBSERVATIONS”>><<replace “#test”>>
<<if 20 <= $mttf && $mttf <= 60>>
<<if $i == 3>>
<<set $scores = $scores + 1.0>>
<<else
<<set $scores = $scores + 0>>
<</if

<<set $r1 = Math.exp((1/$mttf)(1))>>
<<set $r5 = Math.exp((1/$mttf)
(5))>>
<<set $r10 = Math.exp((1/$mttf)(10))>>
<<set $r30 = Math.exp((1/$mttf)
(30))>>
<<set $r60 = Math.exp((1/$mttf)*(60))>>

Great! Now fill in the corresponding reliability for each given time:

Time t (minutes):

t = 1 minute:
Reliability = <<textbox “$response1” “”>>

t = 5 minutes:
Reliability = <<textbox “$response2” “”>>

t = 10 minutes:
Reliability = <<textbox “$response3” “”>>

t = 30 minutes:
Reliability = <<textbox “$response4” “”>>

t = 60 minutes:
Reliability = <<textbox “$response5” “”>>

<<if $visited == 1>>
<<set $j=3>>
<</if
You have $j chances total.

<<if $j eq 0>>
[[CORRECT|Chapter64.2]]
<</if

/* **** set up the limitations *** */\

<<if $j gte 0>>
<<set $visited = 0>>

<<set $j = $j-1>>
<<button “Submit”>>

<<replace “#choice”>><<print "Your answer: ">>
$response1

<<if $response1 gte ($r1 - 0.05) and $response1 lte ($r1 + 0.05) and $response2 gte ($r5 - 0.05) and $response2 lte ($r5 + 0.05) and $response3 gte ($10 - 0.05) and $response3 lte ($r10 + 0.05) and $response4 gte ($r30 - 0.05) and $response4 lte ($r30 + 0.05) and $response5 gte ($r60 - 0.05) and $response5 lte ($r60 + 0.05)>>

/**** Add the score here **** */
<<set $chance = $j>>
<<if $j eq 2>>
<<set $scores = $scores + 5>>
<<elseif $j eq 1>>
<<set $scores = $scores + 4>>
<<elseif $j eq 0>>
<<set $scores = $scores + 3>>
<<endif
\

[[CORRECT|Chapter64.2]]
<<else
[[INCORRECT|Chapter63.2.1]]
<<endif
<</replace
<</button
</span
<<endif
<<else
<<popover ‘noclick’>>Sorry, please try again.<>
<</if
<<set $i = $i - 1>>
<</replace<</link</span
‘’’

just for info

` <- backtick (like the accent on è )
' or ’ <- quote

It’s also this button on the Edit bar:
image

Some points I can see now:

  • <<endif>> is deprecated, use <</if>> instead
  • <<if 20 <= $mttf && $mttf <= 60>>, the variable should be first in a conditional statement → <<if $mttf >= 20 && $mttf <= 60>> (or use gte/lte like you have in the rest of the code)
  • <<textbox>> create strings values, instead of numbers. You can either change the macro with the <<numberbox>> one (documentation), or transform the number in the conditional statement into strings ("20" instead of 20) (not recommended considering the rest of the code)
  • I think it’s been eaten by the formatting, I can’t see the start of the<span> for <<replace “#choice”>> or <<replace “#test”>>
  • <<set $scores = $scores + 0>> there is no need to reset the value of this variable as the same value it has.

Another question: how is $visited set? is it in the previous passage? (I think this might be the reason why $j is not returning as 3…

No it’s not the visited variable. Let me try this. I have two chapters that perform the same function, the code is exactly the same but for whatever reason the first one returns $j as 3 and the second one returns $j as 1, which is my problem. Look and compare the two codes below and tell me what is the difference, or why the second code block might not work.

//FIRST CODE BLOCK
‘’’
/* ***** Link to download the Simio project ****** */
Simio

CHECKPOINT START
After observing the system, enter your observed failure rate. Before calculating the MTTF, click “CHECK MY OBSERVATIONS” below to see if your observations fall within an acceptable range. If they do not, recollect data until your observations satisfy the requirements for a realistic sample.

What is the observed failure rate of table lamps at the assembly station (ie. sent to the rework station)? Report your answer in failures/minute.

Observed Failure Rate: <<textbox “$numfail” “”>>

<<set $i=3>>
<<link “CHECK MY OBSERVATIONS”>><<replace “#test”>>
<<if 0.0167 <= $numfail && $numfail <= 0.05>>
<<if $i == 3>>
<<set $scores = $scores + 1.0>>
<<else
<<set $scores = $scores + 0>>
<</if

<<set $mttf = (1 / $numfail).toFixed(2)>>

Great! What is the mean-time-to-failure (MTTF) of the table lamps at the assembly station?

MTTF: <<textbox “$response1” “”>>

<<if $visited eq 1>>
<<set $j=3>>
<</if
You have $j chances total.
<<if $j eq 0>>
[[CORRECT|Chapter63.2]]
<</if

/* **** set up the limitations *** */
<<set $j = $j-1>>
<<if $j gte 0>>
<<set $visited = 0>>
<span id=“choice”
<<button “Submit”>>

<<replace “#choice”>><<print "Your answer: ">>
$response1

<<if $response1 gte ($mttf - 1) and $response1 lte ($mttf + 1)>>

/**** Add the score here **** */
<<set $chance = $j>>
<<if $j eq 2>>
<<set $scores = $scores + 5>>
<<elseif $j eq 1>>
<<set $scores = $scores + 4>>
<<elseif $j eq 0>>
<<set $scores = $scores + 3>>
<<endif
\

[[CORRECT|Chapter63.2]]
<<else
[[INCORRECT|Chapter62.2.1]]
<<endif
<</replace
<</button
</span
<<endif
<<else
<<popover ‘noclick’>>Sorry, please try again.<</popover
<</if
<<set $i = $i - 1>>
<</replace<</link<span id=“test”</span
‘’’

//SECOND CODE BLOCK
‘’’
/* ***** Link to download the Simio project ****** */
Simio

CHECKPOINT START

That’s correct! Now let’s assume we want to know the reliability of the process at five given times (1, 5, 10, 30, 60 minutes). First input the MTTF you calculated in the last step, and then calculate the reliability at each time t (minutes).

MTTF: <<textbox “$mttf” “”>>

<<set $i=3>>
<<link “CHECK MY OBSERVATIONS”>><<replace “#test”>>
<<if 20 <= $mttf && $mttf <= 60>>
<<if $i == 3>>
<<set $scores = $scores + 1.0>>
<<else
<<set $scores = $scores + 0>>
<</if

<<set $r1 = Math.exp((1/$mttf)(1))>>
<<set $r5 = Math.exp((1/$mttf)
(5))>>
<<set $r10 = Math.exp((1/$mttf)(10))>>
<<set $r30 = Math.exp((1/$mttf)
(30))>>
<<set $r60 = Math.exp((1/$mttf)*(60))>>

Great! Now fill in the corresponding reliability for each given time:

Time t (minutes):

t = 1 minute:
Reliability = <<textbox “$response1” “”>>

t = 5 minutes:
Reliability = <<textbox “$response2” “”>>

t = 10 minutes:
Reliability = <<textbox “$response3” “”>>

t = 30 minutes:
Reliability = <<textbox “$response4” “”>>

t = 60 minutes:
Reliability = <<textbox “$response5” “”>>

<<if $visited eq 1>>
<<set $j=3>>
<</if
You have $j chances total.
<<if $j eq 0>>
[[CORRECT|Chapter64.2]]
<</if

/* **** set up the limitations *** */
<<set $j = $j-1>>
<<if $j gte 0>>
<<set $visited = 0>>

<<button “Submit”>>

<<replace “#choice”>><<print "Your answer: ">>
$response1

<<if $response1 gte ($r1 - 0.05) and $response1 lte ($r1 + 0.05) and $response2 gte ($r5 - 0.05) and $response2 lte ($r5 + 0.05) and $response3 gte ($10 - 0.05) and $response3 lte ($r10 + 0.05) and $response4 gte ($r30 - 0.05) and $response4 lte ($r30 + 0.05) and $response5 gte ($r60 - 0.05) and $response5 lte ($r60 + 0.05)>>

/**** Add the score here **** */
<<set $chance = $j>>
<<if $j eq 2>>
<<set $scores = $scores + 5>>
<<elseif $j eq 1>>
<<set $scores = $scores + 4>>
<<elseif $j eq 0>>
<<set $scores = $scores + 3>>
<<endif
\

[[CORRECT|Chapter64.2]]
<<else
[[INCORRECT|Chapter63.2.1]]
<<endif
<</replace
<</button
</span
<<endif
<<else
<<popover ‘noclick’>>Sorry, please try again.<</popover
<</if
<<set $i = $i - 1
<</replace<</link</span

If you could compare them side by side you can see why my madness is incurring. They are virtually the same save for the if check to see if the answers are correct. NB* In my story sequence the first block of code is one chapter that is followed by the second block which is the next sequential chapter.

Hi,
Could you use the code block please? It’s makes it much easier for use to check the code and test it (because we can copy it). It’s pretty hard to see where the issue lies otherwise. With the way you formatted it, we are missing information (like the <span> start).

Like mentioned in my previous answers, you either need to wrap the code with backtick ( → ` ← this character, which is different from an aposthrophe) or this symbol on the post editor of the forum:
image

Thank you!

//FIRST CODE BLOCK

/* ***** Link to download the Simio project ****** */
Simio

CHECKPOINT START
After observing the system, enter your observed failure rate. Before calculating the MTTF, click “CHECK MY OBSERVATIONS” below to see if your observations fall within an acceptable range. If they do not, recollect data until your observations satisfy the requirements for a realistic sample.

What is the observed failure rate of table lamps at the assembly station (ie. sent to the rework station)? Report your answer in failures/minute.

Observed Failure Rate: <<textbox “$numfail” “”>>

<<set $i=3>>
<<link “CHECK MY OBSERVATIONS”>><<replace “#test”>>
<<if 0.0167 <= $numfail && $numfail <= 0.05>>
<<if $i == 3>>
<<set $scores = $scores + 1.0>>
<<else
<<set $scores = $scores + 0>>
<</if

<<set $mttf = (1 / $numfail).toFixed(2)>>

Great! What is the mean-time-to-failure (MTTF) of the table lamps at the assembly station?

MTTF: <<textbox “$response1” “”>>

<<if $visited eq 1>>
<<set $j=3>>
<</if
You have $j chances total.
<<if $j eq 0>>
[[CORRECT|Chapter63.2]]
<</if

/* **** set up the limitations *** */
<<set $j = $j-1>>
<<if $j gte 0>>
<<set $visited = 0>>
<span id=“choice”
<<button “Submit”>>

<<replace “#choice”>><<print "Your answer: ">>
$response1

<<if $response1 gte ($mttf - 1) and $response1 lte ($mttf + 1)>>

/**** Add the score here **** */
<<set $chance = $j>>
<<if $j eq 2>>
<<set $scores = $scores + 5>>
<<elseif $j eq 1>>
<<set $scores = $scores + 4>>
<<elseif $j eq 0>>
<<set $scores = $scores + 3>>
<<endif
\

[[CORRECT|Chapter63.2]]
<<else
[[INCORRECT|Chapter62.2.1]]
<<endif
<</replace
<</button
</span
<<endif
<<else
<<popover ‘noclick’>>Sorry, please try again.<</popover
<</if
<<set $i = $i - 1>>
<</replace<</link<span id=“test”</span

//SECOND CODE BLOCK

/* ***** Link to download the Simio project ****** */
Simio

CHECKPOINT START

That’s correct! Now let’s assume we want to know the reliability of the process at five given times (1, 5, 10, 30, 60 minutes). First input the MTTF you calculated in the last step, and then calculate the reliability at each time t (minutes).

MTTF: <<textbox “$mttf” “”>>

<<set $i=3>>
<<link “CHECK MY OBSERVATIONS”>><<replace “#test”>>
<<if 20 <= $mttf && $mttf <= 60>>
<<if $i == 3>>
<<set $scores = $scores + 1.0>>
<<else
<<set $scores = $scores + 0>>
<</if

<<set $r1 = Math.exp((1/$mttf)(1))>>
<<set $r5 = Math.exp((1/$mttf)(5))>>
<<set $r10 = Math.exp((1/$mttf)(10))>>
<<set $r30 = Math.exp((1/$mttf)(30))>>
<<set $r60 = Math.exp((1/$mttf)*(60))>>

Great! Now fill in the corresponding reliability for each given time:

Time t (minutes):

t = 1 minute:
Reliability = <<textbox “$response1” “”>>

t = 5 minutes:
Reliability = <<textbox “$response2” “”>>

t = 10 minutes:
Reliability = <<textbox “$response3” “”>>

t = 30 minutes:
Reliability = <<textbox “$response4” “”>>

t = 60 minutes:
Reliability = <<textbox “$response5” “”>>

<<if $visited eq 1>>
<<set $j=3>>
<</if
You have $j chances total.
<<if $j eq 0>>
[[CORRECT|Chapter64.2]]
<</if

/* **** set up the limitations *** */
<<set $j = $j-1>>
<<if $j gte 0>>
<<set $visited = 0>>

<<button “Submit”>>

<<replace “#choice”>><<print "Your answer: ">>
$response1

<<if $response1 gte ($r1 - 0.05) and $response1 lte ($r1 + 0.05) and $response2 gte ($r5 - 0.05) and $response2 lte ($r5 + 0.05) and $response3 gte ($10 - 0.05) and $response3 lte ($r10 + 0.05) and $response4 gte ($r30 - 0.05) and $response4 lte ($r30 + 0.05) and $response5 gte ($r60 - 0.05) and $response5 lte ($r60 + 0.05)>>

/**** Add the score here **** */
<<set $chance = $j>>
<<if $j eq 2>>
<<set $scores = $scores + 5>>
<<elseif $j eq 1>>
<<set $scores = $scores + 4>>
<<elseif $j eq 0>>
<<set $scores = $scores + 3>>
<<endif
\

[[CORRECT|Chapter64.2]]
<<else
[[INCORRECT|Chapter63.2.1]]
<<endif
<</replace
<</button
</span
<<endif
<<else
<<popover ‘noclick’>>Sorry, please try again.<</popover
<</if
<<set $i = $i - 1
<</replace<</link</span

I think it should show up now. Please compare and contrast the two code blocks and take into consideration what was said in the prior replies. thank you!

Yay! Code blocks! Congrats!

But I don’t thing the code you have in those blocks is the same as the code in your file (missing span start and closing brackets)
Could you put the code in your file in the code block?

//FIRST CODE BLOCK

/* ***** Link to download the Simio project ****** */\
<a href="https://pennstateoffice365-my.sharepoint.com/:u:/g/personal/oma110_psu_edu/EST0xNJkfH1Mi7SNp5X5XH4BME6tjCvxsh3NMIY8uZbAbg?e=3ICkXf" target="_blank">Simio</a>

***CHECKPOINT START***
After observing the system, enter your observed failure rate. Before calculating the MTTF, click "CHECK MY OBSERVATIONS" below to see if your observations fall within an acceptable range. If they do not, recollect data until your observations satisfy the requirements for a realistic sample.

What is the observed failure rate of table lamps at the assembly station (ie. sent to the rework station)? Report your answer in failures/minute.

Observed Failure Rate: <<textbox "$numfail" "">>

<<set $i=3>>
<<link "CHECK MY OBSERVATIONS">><<replace "#test">>
<<if 0.0167 <= $numfail && $numfail <= 0.05>>
<<if $i == 3>>
	<<set $scores = $scores + 1.0>>
<<else>>
<<set $scores = $scores + 0>>
<</if>>

<<set $mttf = (1 / $numfail).toFixed(2)>>

Great! What is the mean-time-to-failure (MTTF) of the table lamps at the assembly station?
<span id="choice">

MTTF: <<textbox "$response1" "">>

$j
<<if $visited eq 1>>\
<<set $j=3>>\
<</if>>\
You have $j chances total.
<<if $j eq 0>>
	[[CORRECT|Chapter63.2]]
<</if>>
\
/* **** set up the limitations *** */\
<<set $j = $j-1>>\
<<if $j gte 0>>\
	<<set $visited = 0>>\
<span id="choice">


<<button "Submit">>

<<replace "#choice">><<print "Your answer: ">>
$response1

<<if $response1 gte ($mttf - 1) and $response1 lte ($mttf + 1)>>

/****  Add the score here **** */\
<<set $chance = $j>>
<<if $j eq 2>>
		<<set $scores = $scores + 5>>
<<elseif $j eq 1>>
		<<set $scores = $scores + 4>>
<<elseif $j eq 0>>
		<<set $scores = $scores + 3>>
<<endif>>
\

[[CORRECT|Chapter63.2]]
<<else>>
[[INCORRECT|Chapter62.2.1]]
<<endif>>
<</replace>>
<</button>>
</span>
<<endif>>
<<else>>
<<popover 'noclick'>>Sorry, please try again.<</popover>>
<</if>>
<<set $i = $i - 1>>
<</replace>><</link>><span id="test"></span>

//SECOND CODE BLOCK

/* ***** Link to download the Simio project ****** */\
<a href="https://pennstateoffice365-my.sharepoint.com/:u:/g/personal/oma110_psu_edu/EST0xNJkfH1Mi7SNp5X5XH4BME6tjCvxsh3NMIY8uZbAbg?e=3ICkXf" target="_blank">Simio</a>

***CHECKPOINT START***

That's correct! Now let's assume we want to know the reliability of the process at five given times (1, 5, 10, 30, 60 minutes). First input the MTTF you calculated in the last step, and then calculate the reliability at each time t (minutes).


MTTF: <<textbox "$mttf" "">>

<<set $i=3>>
<<link "CHECK MY OBSERVATIONS">><<replace "#test">>
<<if 20 <= $mttf && $mttf <= 60>>
<<if $i == 3>>
	<<set $scores = $scores + 1.0>>
<<else>>
<<set $scores = $scores + 0>>
<</if>>

<<set $r1 = Math.exp((1/$mttf)*(1))>>
<<set $r5 = Math.exp((1/$mttf)*(5))>>
<<set $r10 = Math.exp((1/$mttf)*(10))>>
<<set $r30 = Math.exp((1/$mttf)*(30))>>
<<set $r60 = Math.exp((1/$mttf)*(60))>>

Great! Now fill in the corresponding reliability for each given time:
<span id="choice">

Time t (minutes):

t = 1 minute: 
Reliability = <<textbox "$response1" "">>

t = 5 minutes: 
Reliability = <<textbox "$response2" "">>

t = 10 minutes: 
Reliability = <<textbox "$response3" "">>

t = 30 minutes: 
Reliability = <<textbox "$response4" "">>

t = 60 minutes: 
Reliability = <<textbox "$response5" "">>

<<if $visited eq 1>>\
<<set $j=3>>\
<</if>>\
You have $j chances total.
<<if $j eq 0>>
	[[CORRECT|Chapter64.2]]
<</if>>
\
/* **** set up the limitations *** */\
<<set $j = $j-1>>\
<<if $j gte 0>>\
	<<set $visited = 0>>\
<span id="choice">
<<button "Submit">>

<<replace "#choice">><<print "Your answer: ">>
$response1

<<if $response1 gte ($r1 - 0.05) and $response1 lte ($r1 + 0.05) and $response2 gte ($r5 - 0.05) and $response2 lte ($r5 + 0.05) and $response3 gte ($10 - 0.05) and $response3 lte ($r10 + 0.05) and $response4 gte ($r30 - 0.05) and $response4 lte ($r30 + 0.05) and $response5 gte ($r60 - 0.05) and $response5 lte ($r60 + 0.05)>>

/****  Add the score here **** */\
<<set $chance = $j>>
<<if $j eq 2>>
		<<set $scores = $scores + 5>>
<<elseif $j eq 1>>
		<<set $scores = $scores + 4>>
<<elseif $j eq 0>>
		<<set $scores = $scores + 3>>
<<endif>>
\

[[CORRECT|Chapter64.2]]
<<else>>
[[INCORRECT|Chapter63.2.1]]
<<endif>>
<</replace>>
<</button>>
</span>
<<endif>>
<<else>>
<<popover 'noclick'>>Sorry, please try again.<</popover>>
<</if>>
<<set $i = $i - 1>>
<</replace>><</link>><span id="test"></span>

To recap, the first code block works, the second one does not. The first code block returns $j as 3 and the second code block returns $j as 1. They are virtually the same except for values and variables, can you help find out what’s wrong?

1 Like

I’ve tested both passages separately, and with both, I can see the name of the variable but not it’s value (as if it was not set in the first place. Like suspected in my first answer, the $visited variable is linked to setting $j. From your code:

<<if $visited eq 1>>\
<<set $j=3>>\
<</if>>\

When I test your code as is:
image

When I set $visited as 1 at the top of the code:
image

(and testing block 2 gives me the same result for $j:
image
)

What I think is happening is the value of $visited is being set somewhere in your code (on other passages) and when that value is not equal to 1 anymore, $j is not set to 3, but follows the rest of the code in the block provided and decreases by 1:

<<set $j = $j-1>>

If you mean to set $j when the player first arrives on the passage, I suggest using:

<<if visited() eq 1>>

but if $visited is related to something else, then please explain its use.
~~

Also, those points in my first answer still stand…

so you’re saying that this is the only instance where $visited needs to be different. I was able to use this code block for some 40 chapters of my story and I’ve never seen this before, I don’t understand why it would only be differnt for this chapter. I can try altering it in this passage again but it hasn’t worked prior. What is the notation " visited() " is that the same as $visited,

What if I did it like this with setting and decrementing the variable, where would i do that in the code?

/* ***** Link to download the Simio project ****** */\
<a href="https://pennstateoffice365-my.sharepoint.com/:u:/g/personal/oma110_psu_edu/EST0xNJkfH1Mi7SNp5X5XH4BME6tjCvxsh3NMIY8uZbAbg?e=3ICkXf" target="_blank">Simio</a>

That's correct! Now let's assume we want to know the reliability of the process at five given times (1, 5, 10, 30, 60 minutes). First input the MTTF you calculated in the last step, and then calculate the reliability at each time t (minutes).

***CHECKPOINT START***

MTTF: <<textbox "$mttf" "">>

<<set $i=3>>
<<link "CHECK MY OBSERVATIONS">><<replace "#test">>
<<if 20 <= $mttf && $mttf <= 60>>
<<if $i == 3>>
	<<set $scores = $scores + 1.0>>
<<else>>
<<set $scores = $scores + 0>>
<</if>>

<<set $r1 = Math.exp((1/$mttf)*(1))>>
<<set $r5 = Math.exp((1/$mttf)*(5))>>
<<set $r10 = Math.exp((1/$mttf)*(10))>>
<<set $r30 = Math.exp((1/$mttf)*(30))>>
<<set $r60 = Math.exp((1/$mttf)*(60))>>

Great! Now fill in the corresponding reliability for each given time:
<span id="choice">

Time t (minutes):

t = 1 minute: 
Reliability = <<textbox "$response1" "">>

t = 5 minutes: 
Reliability = <<textbox "$response2" "">>

t = 10 minutes: 
Reliability = <<textbox "$response3" "">>

t = 30 minutes: 
Reliability = <<textbox "$response4" "">>

t = 60 minutes: 
Reliability = <<textbox "$response5" "">>

<<set $visited = 1>> //NEW LINE
<<if $visited eq 1>>\
<<set $j=3>>\
<</if>>\
You have $j chances total.
<<if $j eq 0>>
	[[CORRECT|Chapter64.2]]
<</if>>
\
/* **** set up the limitations *** */\
<<set $j = $j-1>>\
<<if $j gte 0>>\
	<<set $visited = 0>>\
<span id="choice">
<<button "Submit">>

<<replace "#choice">><<print "Your answer: ">>
$response1

<<if $response1 gte ($r1 - 0.05) and $response1 lte ($r1 + 0.05) and $response2 gte ($r5 - 0.05) and $response2 lte ($r5 + 0.05) and $response3 gte ($10 - 0.05) and $response3 lte ($r10 + 0.05) and $response4 gte ($r30 - 0.05) and $response4 lte ($r30 + 0.05) and $response5 gte ($r60 - 0.05) and $response5 lte ($r60 + 0.05)>>

/****  Add the score here **** */\
<<set $chance = $j>>
<<if $j eq 2>>
		<<set $scores = $scores + 5>>
<<elseif $j eq 1>>
		<<set $scores = $scores + 4>>
<<elseif $j eq 0>>
		<<set $scores = $scores + 3>>
<<endif>>
\

[[CORRECT|Chapter64.2]]
<<else>>
[[INCORRECT|Chapter63.2.1]]
<<endif>>
<</replace>>
<</button>>
</span>
<<endif>>
<<else>>
<<popover 'noclick'>>Sorry, please try again.<</popover>>
<</if>>
<<set $i = $i - 1>>
<</replace>><</link>><span id="test"></span>
<<set $visited = $visited - 1>>  // NEW LINE

No I am saying that the value $visited in the code you provided is the reason $j is getting set or not. From your code, if $visited is not 1, $j will not be set.
You still have not explained where this $visited comes from nor what it is used for in your project. So I am truly at a loss to where to go from where.

Hence, I made an assumption to why you were using that variation (as a way to check if the player had visited that passage before or not), and gave you an alternative notation with the function visited(), a function that counts the ammount of time the current passage has been visited.

Aren’t you the person who coded those passages?
If you are not, you might want to ask the person who provided you with the code what they were trying to do.

====

Unless you explain clearly what $visited is for, and where it is originally set, I really don’t know how to help further.

No i did not code these passages, I have had to adapt and learn to maneuver the code in order to make new things work. If we compare both chapters when I run the first block of code $visited yields 1, whereas in the second block of code $visited yields 0. Do you see anywhere in the following code where or why $visited is being decremented before the next chapter?

/* ***** Link to download the Simio project ****** */\
<a href="https://pennstateoffice365-my.sharepoint.com/:u:/g/personal/oma110_psu_edu/EST0xNJkfH1Mi7SNp5X5XH4BME6tjCvxsh3NMIY8uZbAbg?e=3ICkXf" target="_blank">Simio</a>

***CHECKPOINT START***
After observing the system, enter your observed failure rate. Before calculating the MTTF, click "CHECK MY OBSERVATIONS" below to see if your observations fall within an acceptable range. If they do not, recollect data until your observations satisfy the requirements for a realistic sample.

What is the observed failure rate of table lamps at the assembly station (ie. sent to the rework station)? Report your answer in failures/minute.

Observed Failure Rate: <<textbox "$numfail" "">>

<<set $i=3>>
<<link "CHECK MY OBSERVATIONS">><<replace "#test">>
<<if 0.0167 <= $numfail && $numfail <= 0.05>>
<<if $i == 3>>
	<<set $scores = $scores + 1.0>>
<<else>>
<<set $scores = $scores + 0>>
<</if>>

<<set $mttf = (1 / $numfail).toFixed(2)>>

Great! What is the mean-time-to-failure (MTTF) of the table lamps at the assembly station?
<span id="choice">

MTTF: <<textbox "$response1" "">>

$visited

<<if $visited eq 1>>\
<<set $j=3>>\
<</if>>\
You have $j chances total.
<<if $j eq 0>>
	[[CORRECT|Chapter63.2]]
<</if>>
\
/* **** set up the limitations *** */\
<<set $j = $j-1>>\
<<if $j gte 0>>\
	<<set $visited = 0>>\
<span id="choice">
<<button "Submit">>

<<replace "#choice">><<print "Your answer: ">>
$response1

<<if $response1 gte ($mttf - 1) and $response1 lte ($mttf + 1)>>

/****  Add the score here **** */\
<<set $chance = $j>>
<<if $j eq 2>>
		<<set $scores = $scores + 5>>
<<elseif $j eq 1>>
		<<set $scores = $scores + 4>>
<<elseif $j eq 0>>
		<<set $scores = $scores + 3>>
<<endif>>
\

[[CORRECT|Chapter63.2]]
<<else>>
[[INCORRECT|Chapter62.2.1]]
<<endif>>
<</replace>>
<</button>>
</span>
<<endif>>
<<else>>
<<popover 'noclick'>>Sorry, please try again.<</popover>>
<</if>>
<<set $i = $i - 1>>
<</replace>><</link>><span id="test"></span>

After this code runs for whatever reason $visited yields as 0 in the following chapter:

/* ***** Link to download the Simio project ****** */\
<a href="https://pennstateoffice365-my.sharepoint.com/:u:/g/personal/oma110_psu_edu/EST0xNJkfH1Mi7SNp5X5XH4BME6tjCvxsh3NMIY8uZbAbg?e=3ICkXf" target="_blank">Simio</a>

That's correct! Now let's assume we want to know the reliability of the process at five given times (1, 5, 10, 30, 60 minutes). First input the MTTF you calculated in the last step, and then calculate the reliability at each time t (minutes).

***CHECKPOINT START***

MTTF: <<textbox "$mttf" "">>

<<set $i=3>>
<<link "CHECK MY OBSERVATIONS">><<replace "#test">>
<<if 20 <= $mttf && $mttf <= 60>>
<<if $i == 3>>
	<<set $scores = $scores + 1.0>>
<<else>>
<<set $scores = $scores + 0>>
<</if>>

<<set $r1 = Math.exp((1/$mttf)*(1))>>
<<set $r5 = Math.exp((1/$mttf)*(5))>>
<<set $r10 = Math.exp((1/$mttf)*(10))>>
<<set $r30 = Math.exp((1/$mttf)*(30))>>
<<set $r60 = Math.exp((1/$mttf)*(60))>>

Great! Now fill in the corresponding reliability for each given time:
<span id="choice">

Time t (minutes):

t = 1 minute: 
Reliability = <<textbox "$response1" "">>

t = 5 minutes: 
Reliability = <<textbox "$response2" "">>

t = 10 minutes: 
Reliability = <<textbox "$response3" "">>

t = 30 minutes: 
Reliability = <<textbox "$response4" "">>

t = 60 minutes: 
Reliability = <<textbox "$response5" "">>

$visited

<<if $visited eq 1>>\
<<set $j=3>>\
<</if>>\
You have $j chances total.
<<if $j eq 0>>
	[[CORRECT|Chapter64.2]]
<</if>>
\
/* **** set up the limitations *** */\
<<set $j = $j-1>>\
<<if $j gte 0>>\
	<<set $visited = 0>>\
<span id="choice">
<<button "Submit">>

<<replace "#choice">><<print "Your answer: ">>
$response1

<<if $response1 gte ($r1 - 0.05) and $response1 lte ($r1 + 0.05) and $response2 gte ($r5 - 0.05) and $response2 lte ($r5 + 0.05) and $response3 gte ($10 - 0.05) and $response3 lte ($r10 + 0.05) and $response4 gte ($r30 - 0.05) and $response4 lte ($r30 + 0.05) and $response5 gte ($r60 - 0.05) and $response5 lte ($r60 + 0.05)>>

/****  Add the score here **** */\
<<set $chance = $j>>
<<if $j eq 2>>
		<<set $scores = $scores + 5>>
<<elseif $j eq 1>>
		<<set $scores = $scores + 4>>
<<elseif $j eq 0>>
		<<set $scores = $scores + 3>>
<<endif>>
\

[[CORRECT|Chapter64.2]]
<<else>>
[[INCORRECT|Chapter63.2.1]]
<<endif>>
<</replace>>
<</button>>
</span>
<<endif>>
<<else>>
<<popover 'noclick'>>Sorry, please try again.<</popover>>
<</if>>
<<set $i = $i - 1>>
<</replace>><</link>><span id="test"></span>

I’m sorry then. Aside from my answers above about what I could see was wrong or deprecated, I don’t think I can help you further if you do not know why this was coded this way in the first place or what you are trying to do with the code.
You may want to learn the basics of SugarCube. That will help you understand the different macros used.

Look for a <<set $visited in your project ← that would show you where the value of $visited is changed.
Then look above that line if there is a conditional statement <<if>> affecting setting the value.

2 Likes

Yeah i don’t have time for that, I’ll figure it out. Thank you very much for your time!!

note: When supplying examples of multiple Passages I suggest you either state the Name of the Passage before each example, or use TWEE Notation, as that may help those answering to understand the relationship between the Passage examples you supplied.

If consistent indentation is applied to both of the last two ‘chapter’ code examples you supplied it quickly becomes noticeable that both are missing an </span> tag before the final <<else>> macro call…
eg. a reformatted variation of your 1st example…

<<set $i = 3>>
<<link "CHECK MY OBSERVATIONS">>
	<<replace "#test">>
		<<if 0.0167 <= $numfail && $numfail <= 0.05>>
			<<if $i == 3>>
				<<set $scores = $scores + 1.0>>
			<<else>>
				<<set $scores = $scores + 0>>
			<</if>>

			<<set $mttf = (1 / $numfail).toFixed(2)>>

			Great! What is the mean-time-to-failure (MTTF) of the table lamps at the assembly station?
			<span id="choice">

				MTTF: <<textbox "$response1" "">>

				$visited

				<<if $visited eq 1>>\
					<<set $j = 3>>\
				<</if>>\
				You have $j chances total.
				<<if $j eq 0>>
					[[CORRECT|Chapter63.2]]
				<</if>>
				\
				/* **** set up the limitations *** */\
				<<set $j = $j - 1>>\
				<<if $j gte 0>>\
					<<set $visited = 0>>\
					<span id="choice">
						<<button "Submit">>
							<<replace "#choice">>
								<<print "Your answer: ">>
								$response1

								<<if $response1 gte ($mttf - 1) and $response1 lte ($mttf + 1)>>

									/****  Add the score here **** */\
									<<set $chance = $j>>
									<<if $j eq 2>>
										<<set $scores = $scores + 5>>
									<<elseif $j eq 1>>
										<<set $scores = $scores + 4>>
									<<elseif $j eq 0>>
										<<set $scores = $scores + 3>>
									<<endif>>
									\

									[[CORRECT|Chapter63.2]]
								<<else>>
									[[INCORRECT|Chapter62.2.1]]
								<<endif>>
							<</replace>>
						<</button>>
					</span>
				<<endif>>

			<!-- Missing end </span> tag -->

		<<else>>
			<<popover 'noclick'>>Sorry, please try again.<</popover>>
		<</if>>
		<<set $i = $i - 1>>
	<</replace>>
<</link>>
<span id="test"></span>

eg. a reformatted variation of your 2nd example…

/* ***** Link to download the Simio project ****** */\
<a href="https://pennstateoffice365-my.sharepoint.com/:u:/g/personal/oma110_psu_edu/EST0xNJkfH1Mi7SNp5X5XH4BME6tjCvxsh3NMIY8uZbAbg?e=3ICkXf" target="_blank">Simio</a>

That's correct! Now let's assume we want to know the reliability of the process at five given times (1, 5, 10, 30, 60 minutes). First input the MTTF you calculated in the last step, and then calculate the reliability at each time t (minutes).

***CHECKPOINT START***

MTTF: <<textbox "$mttf" "">>

<<set $i=3>>
<<link "CHECK MY OBSERVATIONS">>
	<<replace "#test">>
		<<if 20 <= $mttf && $mttf <= 60>>
			<<if $i == 3>>
				<<set $scores = $scores + 1.0>>
			<<else>>
				<<set $scores = $scores + 0>>
			<</if>>

			<<set $r1 = Math.exp((1/$mttf)*(1))>>
			<<set $r5 = Math.exp((1/$mttf)*(5))>>
			<<set $r10 = Math.exp((1/$mttf)*(10))>>
			<<set $r30 = Math.exp((1/$mttf)*(30))>>
			<<set $r60 = Math.exp((1/$mttf)*(60))>>

			Great! Now fill in the corresponding reliability for each given time:
			<span id="choice">

				Time t (minutes):

				t = 1 minute: 
				Reliability = <<textbox "$response1" "">>

				t = 5 minutes: 
				Reliability = <<textbox "$response2" "">>

				t = 10 minutes: 
				Reliability = <<textbox "$response3" "">>

				t = 30 minutes: 
				Reliability = <<textbox "$response4" "">>

				t = 60 minutes: 
				Reliability = <<textbox "$response5" "">>

				$visited

				<<if $visited eq 1>>\
					<<set $j = 3>>\
				<</if>>\
				You have $j chances total.
				<<if $j eq 0>>
					[[CORRECT|Chapter64.2]]
				<</if>>
				\
				/* **** set up the limitations *** */\
				<<set $j = $j-1>>\
				<<if $j gte 0>>\
					<<set $visited = 0>>\
					<span id="choice">
						<<button "Submit">>

							<<replace "#choice">>
								<<print "Your answer: ">>
								$response1

								<<if $response1 gte ($r1 - 0.05) and $response1 lte ($r1 + 0.05) and $response2 gte ($r5 - 0.05) and $response2 lte ($r5 + 0.05) and $response3 gte ($10 - 0.05) and $response3 lte ($r10 + 0.05) and $response4 gte ($r30 - 0.05) and $response4 lte ($r30 + 0.05) and $response5 gte ($r60 - 0.05) and $response5 lte ($r60 + 0.05)>>

									/****  Add the score here **** */\
									<<set $chance = $j>>
									<<if $j eq 2>>
										<<set $scores = $scores + 5>>
									<<elseif $j eq 1>>
										<<set $scores = $scores + 4>>
									<<elseif $j eq 0>>
										<<set $scores = $scores + 3>>
									<<endif>>
									\

									[[CORRECT|Chapter64.2]]
								<<else>>
									[[INCORRECT|Chapter63.2.1]]
								<<endif>>
							<</replace>>
						<</button>>
					</span>
				<<endif>>
			
			<!-- missing end </span> tag -->
		
		<<else>>
			<<popover 'noclick'>>Sorry, please try again.<</popover>>
		<</if>>
		<<set $i = $i - 1>>
	<</replace>>
<</link>>
<span id="test"></span>

And the unbalanced nature of both of those examples could be affecting the execution of the Passage code.

The value of the $visited variable can be set before the contents of either of your example ‘chapter’ Passages is processed. And as we have no idea of the Passage Names associated with the two ‘chapter’ examples you have supplied, it is difficult to know what has occurred before either of them were ‘visited’ be the end-user.

Would you be willing and able to supply access to a copy of the Story HTML file you are generating, and the Name of the two ‘chapter’ Passages you want compared, so we can take a deeper look at the issue relating to the setting of the $visited variable.

1 Like

Now the code just returns an Error: macro <> does not exist…