AI Wrote JavaScript Code For Me

0
10


“A Dream Inside A Dream” – Edgar Allen Poe

Do you suppose Synthetic Intelligence (AI, akin to ChatGPT) is able to writing JavaScript code for us to copy-paste right into a Storyline set off? Are you able to work with an AI assistant? I invite you to journey with me to GPT-land. Particularly, we’ll ask OpenAI’s ChatGPT to faux to be an award-winning eLearning developer who is aware of Articulate Storyline and JavaScript and may help us write JavaScript code (whereas we faux to not know JavaScript) [1].

The eLearning Enterprise Downside

As an example we need to type gamers’ names by their scores. We’ve got 10 Storyline textual content variables, referred to as player1, player2, player3, and so forth. and their scores, score1, score2, score3, and so forth. We wish the AI ChatGPT (that internally calls itself the “Assistant”, btw) to put in writing us the JavaScript code to type the gamers by their scores, so we will copy-paste it right into a set off in Storyline. Lastly, we’ll show the sorted “high 5” gamers and their scores on the display screen.

You possibly can comply with alongside by downloading the supply Articulate Storyline file and copy-pasting the Assistant’s code into your individual set off to check it your self. I added the fundamentals within the .story file, so you’ll be able to simply concentrate on the set off itself. The .story file additionally features a answer slide with the ultimate code, however do not take a look at it till the very finish of this text [2]!

Let’s Prime The Assistant

Earlier than leaping into JavaScript code, I wished to examine if the Assistant is aware of about Storyline and its triggers. So, I challenged it with an issue that doesn’t but want JavaScript: I need to choose the highest participant (out of 5) primarily based on rating. If the Assistant is aware of find out how to use Storyline, it will probably clarify the method utilizing a number of triggers and circumstances with none coding.

AI Wrote A JavaScript Code For Me

Spectacular. The Assistant does understand how triggers work in Storyline. It offers particular directions about what to do. Let’s have a look at if the AI Assistant can write the JavaScript code to unravel the issue for us.

The steps appear to make sense (once more, we’re pretending to not know JavaScript). However what in regards to the code?

Copy the next code into your Storyline set off and run the course (I used solely 10 variables to make it simpler as a substitute of 20):

// Loop by way of the 20 rating and participant variables

for (var i = 1; i <= 10; i++) {

var rating = “rating” + i;

var participant = “participant” + i;

// Verify if the present rating is bigger than 0

if (eval(rating) > 0) {

// Add the participant and rating to the sortedPlayers array

sortedPlayers.push({

identify: eval(participant),

rating: eval(rating)

});

}

}

// Type the sortedPlayers array in descending order primarily based on the participant’s rating

sortedPlayers.type(perform(a, b) {

return b.rating – a.rating;

});

// Loop by way of the highest 5 gamers within the sortedPlayers array

for (var i = 0; i < 5; i++) {

var participant = sortedPlayers[i];

// Use Storyline’s built-in options to show the participant identify and rating on the slide

playerName.textual content = participant.identify;

playerScore.textual content = participant.rating;

}

Is The Code Operating?

Did it be just right for you? Not for me. We have to troubleshoot. Open up the Inspector (Ctrl+Shift+I in Chrome) to see the problems. After I click on the “Type” button, the browser shows an error: “player1 is just not outlined.” Let’s ask the Assistant find out how to repair it.

The Assistant’s reply is smart typically, but it surely clearly exhibits two issues: a) it doesn’t perceive how a loop really works, and b) it doesn’t perceive that JavaScript wants GetPlayer() to entry variables in Storyline. If we actually did not know something about JavaScript and Storyline we might be caught at this level. So I “stepped out of my character” and helped the AI Assistant write the JavaScript code somewhat.

To its credit score, the Assistant appears to know this crucial info; it simply ignored it. Ha! It revised its earlier code to incorporate the crucial line. You possibly can copy-paste the code into your set off, overwriting the highest a part of the code.

// Get the participant object

var participant = GetPlayer();

// Loop by way of the ten rating and participant variables

for (var i = 1; i <= 10; i++) {

// Use the GetVar() methodology to get the worth of the “rating” + i variable

var rating = participant.GetVar(“rating” + i);

// Use the GetVar() methodology to get the worth of the “participant” + i variable

var participant = participant.GetVar(“participant” + i);

// Verify if the present rating is bigger than 0

if (rating > 0) {

// Add the participant and rating to the sortedPlayers array

sortedPlayers.push({

identify: participant,

rating: rating

});

}

}

// Type the sortedPlayers array in descending order primarily based on the participant’s rating

sortedPlayers.type(perform(a, b) {

return b.rating – a.rating;

});

// Loop by way of the highest 5 gamers within the sortedPlayers array

for (var i = 0; i < 5; i++) {

var participant = sortedPlayers[i];

// Use Storyline’s built-in options to show the participant identify and rating on the slide

playerName.textual content = participant.identify;

playerScore.textual content = participant.rating;

}

Extra Troubleshooting

Once you take a look at out this model, one other error message is thrown out by the browser: “sortedPlayers variable is just not outlined.” I will prevent from some forwards and backwards with the Assistant, however after a few hiccups it advised me so as to add this line on high of the code:

var sortedPlayers = [];

When you add this line to the highest of your set off, it stops complaining about “sortedPlayers.” I adopted up by asking the Assistant why it did not embody these essential strains, because it was seemingly conscious after the truth that we would have liked them. The reply was a form of apology, but it surely was additionally type of placing the blame on me for not being clearer. Let’s proceed! Wanting on the final couple of strains of code on the backside, I had no concept what the Assistant was pondering…so I requested it to clarify.

And that is the place I began to surprise how a lot of that is purely “imitating” actuality with out really making use of any actual guidelines. The AI Assistant shortly revised and wrote the JavaScript code to learn:

AI Wrote JavaScript Code - Troubleshooting

Let me clarify what I imply by “imitating” actuality, with out entering into the weeds of programming.

The Hassle With The “Participant” Variable

The revised code nonetheless doesn’t work. It has a significant flaw. Why? As a result of you may have a “participant” variable declared up entrance (we name it a worldwide variable), representing the Storyline object. In brief, the unique “participant” variable is what we used to get and set Storyline variables. That works. And so, to start with, the participant.GetVar() and participant.SetVar() features are working properly. Once more, the “participant” variable represents the Storyline object.

…till you get to the code on the backside. Why? As a result of the “var participant = sortedPlayers[i]” line redefines the unique participant variable. In different phrases, whereas up till this level “participant” did have GetVar() and SetVar() features, our Assistant used the identical variable identify (“participant”) once more and set it to the sorted gamers. At that second, we misplaced the unique “participant” variable. And so, this new “participant” does have a “identify” and a “rating” however no SetVar() or GetVar() anymore. It’s now a totally totally different sort of variable.

Now, this could possibly be only a “typo” in a sloppy human programmer’s code (whom I’d not rent). So I wished to examine it with the Assistant. Does it perceive that it can not use the variable identify, “participant”, once more?

Dream Inside A Dream

What I wished to see was if the Assistant would spot the sloppy work and rename the “participant” variable to one thing else, akin to “current_player.” Properly, it did. Sort of. And this can be a large drawback, as a result of what we’re getting from the Assistant is just not actual. It is like a brilliant sensible dream. A dream inside a dream that acts and walks like actuality, however is just not. It’s patterns of actuality which are largely true, so long as you ignore some various information right here and there. It is like a non-duck that walks and talks like a duck.

AI Wrote JavaScript Code - Dream Within Dream

The reason is stable. Verify. It agrees with me about renaming. It did rename the participant variable to “curent_player.” However once more, the Assistant doesn’t perceive that it has launched a brand new drawback. It ought to have saved the “participant.SetVar” as “participant.SetVar.” By renaming all of the phrases “participant” to “current_player”, the code breaks once more as a result of “current_player” doesn’t have a SetVar perform. That is the code it ought to have given me, if it actually understood JavaScript and Storyline:

// Loop by way of the highest 5 gamers within the sortedPlayers array

for (var i = 0; i < 5; i++) {

var current_player = sortedPlayers[i];

// Use the SetVar() methodology to set the worth of a Storyline variable

participant.SetVar(“playerName”, current_player.identify);

participant.SetVar(“playerScore”, current_player.rating);

}

Remaining Downside

Now, there’s additionally a conceptual drawback with this code. It does loop by way of the highest 5 names and scores as we requested. Nonetheless, as a result of it retains setting the identical Storyline variables, “playerName” and “playerScore” within the loop, it retains overwriting them. You’d solely see the fifth worth on the display screen in case you had been to show “playerName” and “playerScore.” Why? As a result of it units “playerName” to the highest participant’s identify, after which units the identical “playerName” to the second, third, fourth, and fifth participant’s identify. It’s fairly quick, so you’d observe solely the final worth, the fifth.

As a way to maintain all 5 high gamers and high scores, it is advisable to retailer these values in several Storyline variables. That is why I created topPlayer1, topPlayer2, and so forth., and topScore1, topScore2, and so forth. within the instance .story file. If you already know JavaScript, you’ll be able to try to repair it. You may discover the answer on the “Resolution” slide within the instance file.

Conclusion

Total, ChatGPT remembers a tremendous quantity of knowledge. The truth that it will probably casually clarify find out how to use Storyline, and seemingly can write JavaScript code with ease, is a glimpse into the longer term. Nonetheless, this future is to date a dream. Completely sensible, however nonetheless a dream. Once you pay shut consideration to particulars, you acknowledge that you just’re in a dream inside a dream. The Assistant’s dream world. Do not consider all the pieces you see, however prepare, as a result of how you’re employed and get issues finished in L&D won’t be the identical…

Quoth the Raven: “Nevermore.”

Assets:

[1] https://chat.openai.com/chat

[2] Supply file

LEAVE A REPLY

Please enter your comment!
Please enter your name here