As I mentioned elsewhere, the learning curve for Hacked is probably a bit steep for new coders. I didn’t just want to give you the answers outright. I’ve included hints to help where you might get stuck, all wrapped in spoiler tags. Teach a man (or woman) to fish! I’ve also included comments in the code blocks, which you can’t do in the app, but may help those who are just starting out with coding. Ignore them when typing them into the hackpad.
Note that while I tend to use the keyword “return” for better readability/understanding, you don’t actually need it, which will save you some keystrokes and score you more points.
[toc heading_levels=”2″]
Power
[spoiler title=”Hint 1″]The output should be the input to the power of 2, but multiply and “pow” function aren’t available yet.[/spoiler]
[spoiler title=”Hint 2″]You can add the number to itself for as many times as needed until you reach the same result.[/spoiler]
[spoiler title=”Hint 3″]A while loop is needed. While loops will repeat for as long as your condition is true, so perhaps loop until your running count is no longer less than the input?[/spoiler]
[spoiler title=”Hint 4″]Make sure you remember to increment your counter to avoid an infinite (endless) loop![/spoiler]
[spoiler title=”Code”]
[code lang=”hack”]while var_a < input {
var_b = var_b + input;
var_a++;
}
return var_b;
// line 1: assume our input is 3. var_a will be our counter, which will default to 0
// line 2: var_b will be 0 the first time it is accessed, but after that it will maintain a running total, which is what we’ll return eventually.
// line 3: we add the value of input to var_b each time the loop runs, e.g. var_b = 0 + 3, var_b = 3 + 3; var_b = 6 + 3;
// line 4: we add 1 to var_a each time we complete the loop.
// back to line 1, we check if the value of var_a is still less than the original input, 3. If so, do the loop again.
// line 5: Eventually when var_a is equal to our input, 3, we stop looping and return the current value of var_b.
[/code]
[/spoiler]
Power 2
[spoiler title=”Hint 1″]As with the last challenge, the output should be the input to the power of 2.[/spoiler]
[spoiler title=”Hint 2″]You’ve now been given the power of the pow function, use it![/spoiler]
[spoiler title=”Hint 3″]Remember how “abs” made our code shorter in Absolute 2? Perhaps “pow” can be used in a similar way?[/spoiler]
[spoiler title=”Code”]
[code lang=”hack”]pow(input, 2);[/code]
[/spoiler]
Length
[spoiler title=”Hint 1″]The input in this example is a list (also known as an array or collection) of items. Each comma indicates a new item in that list. The output should be the number of items in that list.[/spoiler]
[spoiler title=”Hint 2″]We need to count each item, in a similar way to how we counted each complete loop in Power 2[/spoiler]
[spoiler title=”Hint 3″]A foreach loop is used on a list/array/collection and will repeat until it runs out of items in that list (input)[/spoiler]
[spoiler title=”Code”]
[code lang=”hack”]foreach var_a in input {
var_b++;
}
return var_b;[/code]
[/spoiler]
Push it
[spoiler title=”Hint 1″]When given a number as an input, create a list from 0 to that number.[/spoiler]
[spoiler title=”Hint 2″]A while loop is needed until your counter is no longer less than the input[/spoiler]
[spoiler title=”Hint 3″]You will need to initialise your own list by setting it to = [];[/spoiler]
[spoiler title=”Hint 4″]Remember to increment![/spoiler]
[spoiler title=”Code”]
[code lang=”hack”]var_a = [];
while var_b < input {
var_a.push(var_b);
var_b++;
}
return var_a;[/code]
[/spoiler]
19 thoughts to “Hacked app code solutions – Chapter 2 – High School Hack”
Ah, sorry, that was the code for the previous question. A copy and paste bug. I’ve replaced with what should be the correct code.
Is there any way you could explain the answers a bit more? I’m interested in coding, but there aren’t many options (that I know of) for middle schoolers.
Hopefully not stepping on any toes here, but if you’re looking for coding instruction and don’t wanna buy a book, There are a few apps from “sololearn” that are very helpful. I’ve learned the basics of html, css, JavaScript, and C++ from the app. It doesn’t really teach any conceptual aspects, but it’ll teach you the language and how it’s used. Just search “sololearn” in your app store. And no, I’m not affiliated with them at all.
Sorry for the really lengthy delay in reply! I always intended to come back and comment more on the examples but haven’t had the time to do so. I’ll do my best to update this. I hope in the meantime you’ve found a good source of programming tutorials to get you on the right track.
For Power, the first level in Chapter 2, I have the exact same code you have. However, mine claims that for an input of 4, it returns 0.
This is odd, all I can suggest is to double check you have the var_a and var_b in the right place.
The solution for “push it” uses functions that are not being allowed in my app. I cannot type the [] from line one or the .push() on line 3.
Is there a issue with my ap or is there another way to do it?
Very strange. If you click the Help dropdown and select “Latest Hackpad keys” does it show the new keys available of:
. [ ] push
You are probably forgetting to select the ‘.’ Button to gain these options
heyy really nice work…nja
Push it, works for me…
var_a=[];
while var_a.length < input {
var_a.push(var_c++);
};
return var_a;
post-increment in action!
thank's for help Andrew.
Hi ?could u tell me what var a mean by example?thnks
Especially when i m on part 2 while var a…. Function it comes too difficult how to work it,
Some examples of values being assigned to variables:
[hack]foreach var_a in input {[/hack]
input is a list of items, each item in turn will be assigned to var_a for use in the code that follows.
[hack]var_b++;[/hack]
This is shorthand for saying var_b = var_b + 1. In other words, adding 1 to the current value of var_b.
[hack]var_a = [][/hack]
[] is an empty list. so we have set var_a to be equal to an empty list, ready to add items to it.
Hope this is useful!
var_a is short for variable_a.
A variable is a value that you want your code to keep track of. Sometimes you might want to update the same value over and over as your code runs. They can store letters, words, numbers, and more complex Data Types. Common uses for variables are to store the result of some complex code, or to keep track of a counter, or score, for example. More information about Hacked variables and datatypes can be found here. http://www.hackedapp.com/documentation.html#generalinformation
Normally in real coding you are able to name your variable whatever you like, and it’s highly recommended to name it something that describes the value it’s holding, but we don’t have that ability in Hacked, so it can be a pain to remember what variables we used to hold which values.
So something like this:
[hack]
function AddVAT(amount, multiplier) {
return amount * multiplier;
}
var preTaxAmountGBP = 100;
var vatMultiplier = 1.2;
var totalAmount = AddVAT(preTaxAmountGBP, vatMultiplier)
[/hack]
has to become something like this in Hacked:
[hack]
function f1: var_a, var_b {
return var_a * var_b;
}
var_a = 100;
var_b = 1.2;
var_c = f1(var_a, var_b)
[/hack]
Does this help?
Not sure if right place, but the “push it” mission in second chapter of storymode contains a typo “inserts 42 at then end of input”. For me that caused some trouble trying to figure it out first.
Im new at coding and I found this. I want to know what is the use of ++ in this game.
plz tell me about it.
in[3,8]. out[2]
in [4,5,3]. out [3]
in [6] out[1]
plz send me code…
plz give me solution ..
in[3,8]. out[2]
in [4,5,3]. out [3]
in [6] out[1]
as soon as posible send me its code… plzzz