Break The Prison - use array?


#1

First time on CodeCombat and extremely new to programming in any language and wanted to see what the code would look like for this campaign level if an array was defined. Could an array of all of the ‘human’ names be created and then cross referenced in the if statement against the current prisoner name?

Sorry if this has already been addressed in a different area but I looked for the answer in other online resources and came up with nothing. Thanks for any clarification provided.


#2

The array way:

return ['Marcus', 'William', 'OtherName', 'NameN'].indexOf(name)!=-1;

The string way for lazy people. Allowes you to insert the http://deron.meranda.us/data/census-derived-all-first.txt list of names without typing anything manually, just remove the newlines there):

return 'Marcus William OtherName NameN'.indexOf(name)!=-1;  

The optimal performance way:

 if(!this.names){
    //create a hashmap  once
    this.names = {
       Marcus:true,
       William: true,
       OtherName:true,
       NameN: true
 }
}
//see if there is a key in our hashmap
return this.names[name];

By the way, it seems that this level (or the system itself) contains a bug. The ogre with ’ in his name says that he is “MARCUS” !


#3

Thanks for the information and response! Trying to jump in heads first to coding and this community so far has been great!


#4

Grul’thok says his name is "MARCUS" as a trick to try to get you to let him out.


#5

I’m finding this extremely difficult

It says in the guide:

In this level, you’ll learn:

“Using === to tell when two strings of text are equal” But,
I wasn’t aware that === tells when two strings of text are equal, i

“Using if-statements” I didn’t learn to use if-statements

“Using return statements” I didn’t learn to use return statements

“Selecting from multiple spells” I didn’t learn to select from multiple spells

The code I have is:

// Write this isFriend(name) spell to tell friend
from foe.
// Return true for friends’ names, false for ogres’
names.

if(name === “William”)

return true;

if(name === “Krogg”)

return false;  

if(name === “Brack”)

return false;

if(name === “Lucas”)

return true;

if(name === “Gort”)

return false;

if(name === “Robert”)

return true;

if(name === “Marcus”)

return True;

if(name === “Marcus”)

return False;

if(name === “Gordon”)

return True;

There’s an error on the Gordon one, they don’t break him out and it says: Time 24.2: ReferenceError.


#6

And this is one of the reasons I like python:

return name in 'Marcus William Lucas Phoebe Gordon'

#7

Your problems are all caused by case-sensitivity. In this case, True and False are capitalized when they shouldn’t be, and “Marcus” should be “MARCUS”.