# The Elf Code

Location: Dining Room

### Introduction

Ribb Bonbowford tells us about this challenge

Hello - my name is Ribb Bonbowford. Nice to meet you!

Are you new to programming? It's a handy skill for anyone in cyber security.

This challenge centers around JavaScript. Take a look at this intro and see how far it gets you!

Ready to move beyond elf commands? Don't be afraid to mix in native JavaScript.

Trying to extract only numbers from an array? Have you tried to filter?

Maybe you need to enumerate an object's keys and then filter?

Getting hung up on number of lines? Maybe try to minify your code.

Is there a way to push array items to the beginning of an array? Hmm...

...

### Solutions

#### Level 1

Program the elf to the end goal in no more than 2 lines of code and no more than 2 elf commands.

Game Map ``````elf.moveLeft(10)
elf.moveUp(10)
``````

#### Level 2 - Trigger The Yeeter

Description

Move to the lever, `elf.get_lever(0)`, and manipulate the resulting data however it asks, and send the answer to elf.pull_lever(answer). The yeeter should release, and you can move freely.

Click on the object help and current level object icons for examples on how to complete this task.

Program the elf to the end goal in no more than 5 lines of code and no more than 5 elf command/function execution statements in your code.

Game Map LEVER #0 OBJECTIVE

Objective:

Add 2 to the returned numeric value of running the function `elf.get_lever(0)` .

For example, if you wanted to multiply the value by 3 and store to a variable, you could do: `var sum = elf.get_lever(0) * 3`

Then submit the sum using: `elf.pull_lever(sum)`

Note

If you submit a correct answer to `elf.pull_lever(answer)`, then the lever and its corresponding trap will be disabled.

In order to run `elf.pull_lever(answer)` with lever (#0), you must be standing in its grid square located at (x6,y12).

You must specify the lever number when using `elf.get_lever(0)`

``````elf.moveLeft(6)
var sum = elf.get_lever(0) + 2
elf.pull_lever(sum)
elf.moveLeft(4)
elf.moveUp(10)
``````

#### Level 3 - Move To Loopiness

Description

Pick up all of the lollipops!

Program the elf to the end goal in no more than 4 lines of code and no more than 4 elf command/function execution statements in your code.

Game Map ``````for (i = 0; i < 3; i++) {
elf.moveTo(lollipop[i])
}
elf.moveUp(1)
``````

#### Level 4 - Up Down Loopiness

Description

Note

Using another `for` loop could reduce how many elf function statements are used.

Hint

Using `elf.moveLeft(40)` will move your elf as far as possible before hitting an obstacle or the end of the screen. Use however high a number you think you need!

Program the elf to the end goal in no more than 7 lines of code and no more than 6 elf command/function execution statements in your code.

Game Map ``````for (i = 0; i < 3; i++) {
elf.moveUp(40)
elf.moveLeft(3)
elf.moveDown(40)
elf.moveLeft(3)
}
elf.moveUp(40)
``````

#### Level 5 - Move To Madness

Description

Hint

Experiment with the `elf.moveTo()` function. You might be able to get two-in-one if you move to `munchkin`.

Click on the munchkin in the CURRENT LEVEL OBJECTS window to see the kind of answer the munchkin is looking for in this challenge.

Program the elf to the end goal in no more than 10 lines of code and no more than 5 elf command/function execution statements in your code..

Game Map MUNCHKIN #0 OBJECTIVE

Objective:

Use `elf.ask_munch(0)` and I will send you an array of numbers and strings similar to:

`[1, 3, "a", "b", 4]`

Return an array that contains only numbers from the array that I give you. Send your answer using `elf.tell_munch(answer)`.

Note

If you submit a correct answer to `elf.tell_munch(answer)`, then the munchkin will become friendly.

In order to run `elf.tell_munch(answer)` with elf (#0), you must be standing within 1 grid square of the munchkin.

You must specify the elf number when using `elf.ask_munch(0)`

``````elf.moveTo(lollipop)
elf.moveTo(lollipop)
var value = elf.ask_munch(0)
var answer = value.filter(elem => typeof elem === 'number')
elf.tell_munch(answer)
elf.moveUp(3)
``````

#### Level 6 - Two Paths, Your Choice

Description

Note

There are two paths here for you to choose. Choosing the lever may take more steps but might be easier to solve.

Program the elf to the end goal in no more than 15 lines of code and no more than 7 elf command/function execution statements in your code.

Game Map MUNCHKIN #0 OBJECTIVE

Objective:

Use elf.ask_munch(0) and I will return a JSON object similar to:

{ "2ghd3":327, "pwmcojfd":23, "ivntirc":"asjkdhfg", "qpwo":76, "szixuchv":"lollipop", "aiusywt":4, "xmzxcv":"sdfhj", }

Use elf.tell_munch(answer) to tell me the name of the key with a value of lollipop.

In this example, the solution would be elf.tell_munch("szixuchv").

Note

If you submit a correct answer to elf.tell_munch(answer), then the munchkin will become friendly.

In order to run elf.tell_munch(answer) with elf (#0), you must be standing within 1 grid square of the munchkin.

You must specify the elf number when using elf.ask_munch(0)

``````for (i = 0; i < 4; i++) {
elf.moveTo(lollipop[i])
}
elf.moveLeft(8)
elf.moveUp(2)
var value = elf.ask_munch(0)
var answer = Object.keys(value).find(key => value[key] === 'lollipop');
elf.tell_munch(answer)
elf.moveUp(3)
`````` #### Level 7 - Yeeter Swirl

Description

About

Follow the swirl being careful not to step on any traps (or get yeeted off the map).

Note

`elf.moveTo(object)` has been disabled for this challenge.

Hint

Use loops and an incrementing count to take the exact number of steps.

Program the elf to the end goal in no more than 25 lines of code and no more than 10 elf command/function execution statements in your code.

Game Map LEVER #0 OBJECTIVE (#0 to #6 are similar, incrementing submitted number, 0 through 6)

Objective:

Submit `elf.pull_lever(0)` and I'll lift the next bridge.

Note

If you submit a correct answer to `elf.pull_lever(answer)`, then the lever and its corresponding trap will be disabled.

In order to run `elf.pull_lever(answer)` with lever (#0), you must be standing in its grid square located at (x6,y9).

You must specify the lever number when using `elf.get_lever(0)`

MUNCHKIN #0 OBJECTIVE

Objective:

Create a function that will accept one argument.

For example:

``````function YourFunctionNameHere(one_argument) {
// some function code will go here
return some_desired_data
}
``````
Your created function will be passed a randomized array containing arrays which contain strings and numbers.

For example:

``````[
[1,"sdff",2,9,"olidfhj",6],
[2,5,1,"jdhgwe",4],
["wyuier",2,2,9,2,"jfghwgfb",5],
[4,"bnwc",9]
]
``````
Your created function must be able to iterate over this randomized array and each of its child arrays and return the total sum of adding all of the numbers in all of the child arrays.

Once you have created this function and are sure it will return the desired sum, pass your created function as an argument to me:

`elf.tell_munch(YourFunctionNameHere)`

If your passed function works properly, I will become friendly and move out of your way.

Note

If you submit a correct answer to `elf.tell_munch(answer)`, then the munchkin will become friendly.

In order to run `elf.tell_munch(answer)` with elf (#0), you must be standing within 1 grid square of the munchkin.

You must specify the elf number when using `elf.ask_munch(0)`

``````function summer(input) {
var sum = 0,
flattened = [].concat.apply([], input);
for (i = 0; i < flattened.length; i++) "number" == typeof flattened[i] && (sum += flattened[i]);
return sum
}
count = 0;
do {
switch (count % 4) {
case 0:
elf.moveDown(count + 1);
break;
case 1:
elf.moveLeft(count + 1);
break;
case 2:
elf.moveUp(count + 1);
break;
case 3:
elf.moveRight(count + 1)
}
elf.pull_lever(count), count++
} while (count < 7);
elf.moveRight(count + 1), elf.moveUp(2), elf.moveLeft(4), elf.tell_munch(summer), elf.moveUp(2);
``````

#### Level 8 - For Loop Finale

Description

About

Follow the zig-zag being careful not to step on any traps (or get yeeted off the map).

Note

The `elf.moveTo(object)` function has been disabled for this challenge.

Hint

Use loops and track incrementing values to take the exact number of steps.

Program the elf to the end goal in no more than 40 lines of code and no more than 10 elf command/function execution statements in your code.

Game Map LEVER OBJECTIVE

Objective

Goal

Your task is to sum up all the values of `elf.get_lever(x)` for the lever you are on and intend to `elf.pull_lever(x)` and for all the previous levers.

For example, if you wanted to `elf.pull_lever(0)`, you could use:

``````var num0 = elf.get_lever(0)
elf.pull_lever(num0)
``````
Then, if you wanted to pull lever 1, you would expand the previous example to:
``````var num0 = elf.get_lever(0)
elf.pull_lever(num0)
elf.moveUp(2)
elf.moveLeft(3)
var num1 = elf.get_lever(1)
elf.pull_lever(num0 + num1)
``````
Likewise, if you wanted to pull lever 2:
``````var num0 = elf.get_lever(0)
elf.pull_lever(num0)
elf.moveUp(2)
elf.moveLeft(3)
var num1 = elf.get_lever(1)
elf.pull_lever(num0 + num1)
elf.moveUp(2)
elf.moveRight(5)
var num2 = elf.get_lever(2)
elf.pull_lever(num0 + num1 + num2)
``````
Important - The above examples are only an example to show you the logic behind the levers for this level. However, using the above examples exactly is not likely to work as you may only use 10 elf function execution statements in your code. As such, using loops will be necessary.

Result:

Successfully pulling a lever will trigger the next bridge to be raised so that you can pass safely.

Hint

The above examples should illustrate what the intent is but remember that you are limited on the number of possible elf function executions you can use in your code. As such, using loops is a good idea.

Note

If you submit a correct answer to `elf.pull_lever(answer)`, then the lever and its corresponding trap will be disabled.

In order to run `elf.pull_lever(answer)` with lever (#0), you must be standing in its grid square located at (x7,y12).

You must specify the lever number when using `elf.get_lever(0)`

MUNCHKIN #0 OBJECTIVE

Objective:

Create a function that will accept one argument.

For example:

``````function YourFunctionNameHere(one_argument) {
// some function code will go here
return some_desired_data
}
``````
Your created function will be passed a randomized array containing json objects as it's one_argument.

For example:

``````[
{"qpwo":76,"szixuchv":"lollipop","aiusywt":4,"xmzxcv":"sdfhj"},
{"aklgr":234,"yuigwer":"qwuieyr","xcmvp":21},
{"qopwue":"aosdfh","szixuchv":45,"hfas":35,"qiowphq":2,"zxc":"bgqwuygfvq"},
{"zxcvzkj":"azga","wieyr":36},
]
``````
Your created function must be able to iterate over this randomized array of json objects and return the string name of the key with a string value of lollipop (see above example).

Once you have created this function and are sure it will return the desired string key name, pass your function as an argument to me:

elf.tell_munch(YourFunctionNameHere)

If your passed function works properly, I will become friendly and move out of your way.

Note

If you submit a correct answer to `elf.tell_munch(answer)`, then the munchkin will become friendly.

In order to run `elf.tell_munch(answer)` with elf (#0), you must be standing within 1 grid square of the munchkin.

You must specify the elf number when using `elf.ask_munch(0)`

``````function json_key(input) {
for (var i = 0; i < input.length; i++) {
answer = Object.keys(input[i]).find(key => input[i][key] == 'lollipop')
if (answer) {
return (answer)
}
}
return answer
}
lever_count = 0
for (i = 0; i < 6; i++) {
if (i % 2) {
elf.moveLeft(i * 2 + 1)
} else {
elf.moveRight(i * 2 + 1)
}
lever_count += elf.get_lever(i)
elf.pull_lever(lever_count)
elf.moveUp(2)
}
elf.tell_munch(json_key)
elf.moveRight(40)
`````` 