Counting votes

Twine Version: 2.7.0 (Sugarcube 2.36.1)

I need an idea, an explanation why it doesn’t work and what other alternatives are there?

In sugarcube documentation, it gives me following example:

 Given: $fruits = ["Apples", "Oranges", "Plums", "Oranges"]
$fruits.count("Oranges")     → Returns 2

therefore I change it to:

<<set $numero = random(1, 3)>><<set $Vikavote = random(1, 3)>><<set $Leovote = random(1, 3)>>
  
$vcount = ["$numero", "$Vikavote", "$Leovote"]
<<print $vcount.count("1")>>

but the error message gives me the following:

Error: <<print>>: bad evaluation: Cannot read properties of undefined (reading 'count') 
<<print $vcount.count("1")>>

ChatGPT tells me it is because it reads/parses the variable, not the value. If so, what other possibilities are there that it counts the amount of votes? I am out of ideas. Or, at least, is there a redundant command for counting the amount of votes?

Also, I tried with this one from sugarcube documentation:

$numbers = [1, 2.3, 4, 76, 3.1]
$numbers.countWith(Number.isInteger)  → Returns 3

but I have no idea what kind of command do I write? …countWith(Number.isAmount)?

1 Like

Hoo boy. Don’t trust ChatGPT. LLMs give you text that sounds like a reasonable answer. They’re very good at sounding good but they have no way whatsoever of telling whether their output is true or not. That’s just not how they work. You can even tell it “oh, that’s wrong” and it’ll give you a plausible response “oh, sorry, what I really meant was blah blah blah” but even that is just an answer that sounds good: that’s what you say when someone tells you you’re wrong. It doesn’t actually mean the new thing is any more likely to be right…

Anyway.

Two problems here: you’re not actually setting vcount. You need a <<set>> command for that, not just a bare $vcount = whatever. That’s why you’re getting that error: since $vcount is undefined, it’s trying to print undefined.count("1").

But then once you set it, you have quotes around the variable names, so you’d be setting vcount to an array with the variables’ names, not their contents. It’s just like <<set $vcount = ["Fred", "Bill", "Bob"]>> except each string has a dollar sign at the beginning.

This is a particularly sneaky bug because if you try <<print $vcount>> you’ll see the correct numeric values, because it prints “$numero, $Vikavote, $Leovote” and then the printing interprets those as variables and prints their values instead. So it looks right even though it’s wrong. In Twine, if you hit the Test button to run your story in Test mode, and click the bug (beetle? ladybug?) at the bottom right corner, you can add a “watch” on $vcount and it’ll show you the true value, I guess? Write $vcount in the Add textbox and then click the plus button.

So what you actually want is <<set $vcount = [$numero, $Vikavote, $Leovote]>>.

Then your $vcount.count(1) should work. Oh, wait. You actually have $vcount.count("1") which is counting the number of strings that have only the character 1. That’s different from the number 1.

6 Likes

Monsieur. I thank you! It works perfectly and smooth now.
Now I shall remind myself to have each variable defined beforehand.

Perfect. Gracias.

P.S.: About ChatGPT. Yeah. I have found it out too that it rarely works. Yet, sometimes, I estimate around in 10 to 20% of cases, it works. And I learn a bit new codes, especially JavaScript. And some questions about CSS, it helps.

1 Like