Twine Version: 2.3.13
Story Format: Sugarcube 2.34.1
Hello,
I’m using twine to create a tool where users can generate emails to job applicants by clicking through various options for each phrase (with the cycle macro). Depending on their choices the resulting email could range from an enthusiastic job offer to a brutal rejection.
That part all works fine. Once the user has finalised their email I want them to be able to click a button (or similar) to make it shorter. For example:
Dear candidate,
Thank you for your interest in working with us. We are writing to inform you that you didn’t get the job.
becomes:
Dear candidate,
You didn’t get the job.
I’ve got this working in Javascript but am having trouble fitting it into Twine. My code manipulates the array of variables but I’m not sure how to pass these through the function and back. Here are my attempts so far:
<<set $email to [$a, '<br><br>', $b, $c, $d, '<br><br>', $e, $f, $g, $h, $i]>>
<<set $email = State.variables.baseEmail>>
<<set $shortemail = State.variables.shortEmail>>
<<button "make it shorter" "make it shorter">><<script>>shorten();<</script>><</button>>
—
<<for _i=0; _i<$shortemail.length; _i++>>
<<print $shortemail[_i]>>
—
Then in the JS section:
window.shorten = function shortenEmail () {
//create new modified strings to insert into new array
var stringA = baseEmail[4];
newStringA = stringA[0].toUpperCase() + stringA.slice(1);
var stringB = baseEmail[7];
newStringB = stringB[0].toUpperCase() + stringB.slice(1, -1);
//make edits
var shortEmail = [...baseEmail]
shortEmail.splice(2,3, newStringA);
shortEmail.splice(4,2, newStringB);
shortEmail.splice(4,2, newStringB);
shortEmail.splice(6,0);
return shortEmail;
}
Apologies if this is terrible code, I’ve only been learning for a few weeks. I did look into using Sugarcube’s array methods but couldn’t see an equivalent to splice.
Any suggestions would be greatly appreciated. Cheers!