Band Names Generator explainer


#1

The aim of this puzzle: Build a band-name generator that outputs at least 9 different band name options.
Walkthrough of the solution: To solve this puzzle you’ll need to add words to inside the adjective array [] and inside the noun array []. Once you’ve added words to the two arrays, you’ll need to update what is being printed out to reference the variable adj and the variable noun. The first for loop (the outer for loop) will repeat each word in its array (the adj array) once for every word within the second for loops array (the inner loop). This happens because the print() function is within the inner for loop, meaning its looping through each word in the noun array while also looping through each word in the adj array.
Sample code solution:
(Tap below to reveal)

for (var adj of [
  'Lost',
  'Raging',
  'Fake',
  'Anonymous'
]) {
  for (var noun of [
    'Pharmacists',
    'Squids',
    'Twins',
    'Feels'
  ]) {
    print('The '+adj+' '+noun);
  }
}

JavaScript Concepts: Data Structures (Arrays), Code Block (for loop), Loops, Variable Declaration, Binary Expression (+), Calling Functions, Identifiers
Grasshopper Concepts: print()


#2

Hey guys,
i realy enjoy the app! Thanks for that!
Can you tell me, why it’s necessary to write at the print line … " " + noun? Why not just …+noun?
Thanks for help


#3

Hi Emce,
Are you referring to:
adj+ ' ' +noun);?
The string between adj and noun is to provide a space in the output so it can be:
The Crazy Coders
instead of:
The CrazyCoders
Hope this helps! :smile:


#4

Thank you!
Now I get it . I deleted the ’ ’ but it didn’t work. I forgot to delete the _


#5

Thank you for clearing that up. :smile:


#6

@Grasshopper_Frankie
Please explain the use of different brackets ? When we use (), [],{} ?
I’m confused with the use of brackets.


#7
  • () – Parentheses

    • Used to group together math operations

      • (5 - 4) - 1 === 1 - 1 === 0
      • 5 - (4 - 1) === 5 - 3 === 2
    • Used to identify inputs (of a Function, If Statement, Loop)

      • print('Use this string'); – The thing inside the () is the argument for the print function, part of setting up a function call is giving it it’s arguments
      • if (x > 1) {...} – The thing inside the () is the test, part of the setup for an If Statement is telling it when it should or shouldn’t run the code block {}
      • for (var item of array) {...} – The thing inside the () tell the loop how many times to run, part of the setup of a For Loop
  • [] – Square Brackets

    • Used to create an array

      • var pets = ['dog', 'cat', 'fish'] – The items (separated by commas) between the [] are the elements of a new array called pets
    • Used to select an item from an array

      • pets[1] – selects ‘cat’ from the pets array because the numbering starts at 0
  • {} – Curly Brackets

    • Used to group lines of code together in a Code Block

      • for (...) {
          print('first command in block');
          print('second command in block');
        }
        
        – the whole block of code is dependent on the For Loop, otherwise the code would assume just the first print was inside the For Loop
    • Used to create an object

      • var addressBook = {
          alice: '101 Main St.',
          bob: '321 Fourth Ave.'
        }
        
        – the brackets contain 2 pairs of name: value

The confusing part of this puzzle’s code is that there are nested for loops, and each loop has an array as part of the setup. A single For Loop has:

for (setup) {
  actions
}

A nested For Loop means the action of one For Loop is to run another Loop:

for (setup) {
  for (setup2) {
    actions2
  }
}

If the setup is “loop through the items in this array”, it looks like:

for (var item of array) {
  for (setup) {
    actions2
  }
}

If that array isn’t stored in a separate variable but instead created right in the setup:

for (var item of [thing1, thing2, thing3]) {
  for (setup) {
    actions2
  }
}

A similar setup for the inner loop gives you:

for (var item of [thing1, thing2, thing3]) {
  for (var element of [stuff1, stuff2, stuff3]) {
    actions2
  }
}

In the app, arrays elements are arranged vertically just for organization:

for (var item of [
  thing1,
  thing2,
  thing3
]) {
  for (var element of [
    stuff1,
    stuff2,
    stuff3
  ]) {
    actions2
  }
}

–Frankie


#9

Thank you :slight_smile: for explaining use of brackets. It really helped me to understand this topic very well.