Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
I'm coding in AppLab, the coding language used in Code.Org.

I'm trying to get the computer to display, in a text box, one of four text variables, titled "Forehand, Backhand, Slice, or Smash." The problem I'm having is that the only Randomizer function available is that of the RandomNumber(#,#), which as you can probably guess, only accepts numbers. Therefore, I attempted to assign each text variable with a number, and to have the computer randomly select a number between 1-4, where each number is assigned a text variable like Forehand, Backhand, etc.

I've no idea how to do it though. Any help?

What I have tried:

I attempted to assign each text variable with a number, and to have the computer randomly select a number between 1-4, where each number is assigned a text variable like Forehand, Backhand, etc.
Posted
Updated 11-Jan-21 15:01pm

You have your rand [1,2,3,4]
you have your array ["word","word","word","word"]

"triforce" you have your solution

array[rand] = "word"

That is true in AppLab as it is in many languages. "triforce" my guess is that the execution is not your issue, but understanding.

Here is my reference:App Lab Docs | Code.org[^]
Learn to google, then learn to question, and always learn ^_^

Regards
Andy ^_^
 
Share this answer
 
Comments
Andy Lanng 26-Apr-17 12:49pm    
extra credit: based on the send state: ["forehand", "backhand", "lob",...], limit the resend. should take two more lines (ignoring syntax) to achieve, but not limited to
I have a solution

Just make x a random number between 1 and 4 and use an if loop that changes x if each number.

var x = randomNumber(1, 4);
if (x == 1) {
x = "forehand";
} else if ((x == 2)) {
x = "backhand";
} else if ((x == 3)) {
x = "slice";
} else if ((x == 4)) {
x = "smash";
}
setText("label1", x);

Hope this helps!!!
 
Share this answer
 
v2
Comments
CHill60 12-Jan-21 6:42am    
Reason for my downvote - there is no such thing as an "if loop".
Solution 1 is far more elegant and readable and was posted when the OP still needed help
Member 15043457 12-Jan-21 14:01pm    
There is an if loop in the control section.
CHill60 13-Jan-21 4:16am    
randomNumber returns a single value. Then there is a set of if statements. There are no loops at all in your code.
itititititititititit 14-Jan-21 11:35am    
yes there is have u tried going to text mode and pasting it in?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900