Silly Sentences explainer


#1

The aim of this puzzle: Create a list (an array) of things an owl might do at midnight, then randomly select one and print it out.

Walkthrough of the solution: The variable word should hold a list (an array []) rather than a single word. To replace the string with an array, tap on 'quacks' to highlight it (you should still see the little keyboard icon) and then tap the array key []. Once you’ve created the array, you put items inside of it. For this puzzle, it should store strings. Use the str key to add two different strings to the array.

You also need to randomly pick an item from the array and there are two different ways to do this! You can use the pickRandom() function when you reference the word variable in the print statement, to make something that looks like print('the owl ' + pickRandom(word) + ' at midnight') OR you could pick the random thing while you’re creating the variable, by putting the array inside the function, like var word = pickRandom(['quacks', 'hoots', 'dances']).

Sample code solution:
(Tap below to reveal)

var word = pickRandom([
  'hoots',
  'quacks',
  'howls',
  'dances'
]);
print('the owl ' + word + ' at midnight');

JavaScript Concepts: Data Structures (arrays), Variable Declaration, Calling Functions, Identifiers, Binary Expressions (+)
Grasshopper Concepts: pickRandom(), print()


#2

I’m actually learning something :joy::joy::joy: I will learn c++ next


#3

@Aaron_Lankford


#4

Thank you have a good evening.


#6

Hi When I use only [] end when i use pickRandom()


#7

i cannot get rid of the final comma when inserting values in the array and always get an error. How can I insert the last string without the comma?


#8

@Another_Account The comma shouldn’t be causing any errors as an extra comma at the end of the last string in an array should just ignore it. Are you able to post a picture of your error?

Or, you can compare your code to this one and see if that helps:


#9

Thanks Jennifer
I got it to work. I was following the instructions and not thinking it through myself.

Jan


#10

Hi @Gabriela_Villar :wave: ,

it depends on what you intend to achieve. For this puzzle, you need a single random word inside an array, thats why we use pickRandom().
But there are other cases, in which you’ll need a specific word in a specific position in the array. Let’s say you need the third element (the 3rd word) of it, then you can write something like arrayName[3] (or, technically, arrayName[2]).
But these brackets I used are for referencing the element inside the array, which are different from [ ] used for creating the array. Maybe these latest are the ones you questioned of.
In effect, to declare and populate an array, you can simply do something like

var arrayName = [ ‘some string’, ‘NaN’, ‘a word’ ];

(forget pickRandom() in this situation)


#11

What’s wrong with this?


#12

Hi @Vicmatmar,

Your solution is great, there is nothing wrong with it.

This is a good approach and we’ve updated this lesson to accept this solution so it will work after the next update.

-Mike