0 votes
by (2.2k points)

 So the code I'm attempting to use is not working, greyelf so kindly helped out with the first part of the code, I added the .change because I wanted to assign a name in the case that there wasn't input from the previous passage.

<% if (character.name == "") .change="character.name = "name I choose"" { %> text in the event nothing is input and it'll change the name to whatever I put <% } %>
<% ifelse { %> all text if a name was input in the previous passage <% } %>

I figure it's the change token but I can't find something that would work, I was hoping it would work somehow lol. I don't really understand how to use two functions in the same thing so I was hoping someone could point out what I'm doing wrong in detailed steps. 

Did I even use the if else correctly?

Also, is there a way to select the character name from a random variable? I want to list a bunch of names elsewhere to assign them to a variable that would take place of "name I choose" so that way even if the player doesn't want to input a name it's not the same name every time. 

1 Answer

0 votes
by (63.1k points)

I'm not 100% sure, but you probably want something like this: 

<% 
if (!character.name) {
    character.name = 'Bob'; 
} else {
    character.name = character.name.trim();
}
%>

I'm not sure what the whole change thing is about. 

by (2.2k points)

sorry, the change thing comes from 

<input onchange="character.name = this.value">

that I used in a previous passage to collect user input. What I want is to change the name only in the case that the user never input a name. How would I make the thing recognize to change the name to something different with the lack of an input? From your method it seems to just change the name without recognizing a lack of input? I don't even know for sure. 

by (63.1k points)
It will recognize a lack of input. An empty string coerces to false. Assuming that input is working, putting my code in the next passage should set the name to 'Bob' if no name is entered.

Or are you saying you tried it and it didn't work?
by (2.2k points)
I am just trying to understand it actually. I'd like to try it but for some reason my thingy majig isn't playing, testing, or publishing it to file so I'm a little incapable at the moment.
by (63.1k points)
if (!character.name) { // if character.name is not true. Empty strings (''), zero, null, and undefined are considered "falsey" in JavaScript, meaning they come back false when evaluated as booleans (which is what if does)
    character.name = 'Bob'; // since we've established that the name is falsey (probably undefined or an empty string, set it to a default. 
} else { 
    character.name = character.name.trim(); // this is optional. The trim() method just cleans up the string by removing leading and trailing white space, and is a thing I usually do on user-entered strings. Not necessary, but a good practice. 
}

Here's the code with explanatory comments. 

by (2.2k points)
Oh! Thank you! that's super helpful
...