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 need it for the final output, which will save you some keystrokes and score you more points.
[toc heading_levels=”2″]
Increment me
[spoiler title=”Hint”]add 1 to input[/spoiler]
[spoiler title=”Code”]
[code lang=”hack”]input + 1; // Simple, add 1 to the original input[/code]
[/spoiler]
Positive
[spoiler title=”Hint”]if input is more than 0, then return true, else false.[/spoiler]
[spoiler title=”Code”]
[code lang=”hack”]input > 0; // You are stating that your input is more than 0. This will answer whether that statement is true or false.[/code]
[/spoiler]
Absolute
[spoiler title=”Hint”]if your input is a negative number, then negate the negative to turn it into a positive…[/spoiler]
[spoiler title=”Code”]
[code lang=”hack”]if input < 0 {
return -input;
}
return input;
// line 1: We turned a statement into a condition, Only perform the next step IF the above statement (input < 0) is true.
// line 2: reverse the negative input, e.g. -2 becomes 2.
// line 4: otherwise, if it’s 0 or more, don’t change it.
[/code]
[/spoiler]
Absolute 2
[spoiler title=”Hint”]the function “abs” will do the same as your code in “Absolute”, but with much less code.[/spoiler]
[spoiler title=”Code”]
[code lang=”hack”]abs(input); // Imagine that inside abs is the code from the previous example, hidden away.
[/code]
[/spoiler]
5 thoughts to “Hacked app code solutions – Chapter 1 – The Hackpad”
Hellow there i find myself in front of a problem. What are hidden case meant too. Im really confused about the mwchanic of the game.
Hidden cases in this one is if input are any other number
You test your code by comparing your outputs to the “cases” presented. “Hidden cases” are extra examples to help you prove that your solution really does output the expected result for all likely inputs.
Sometimes you may find your solution works for the initial cases, but doesn’t pass hidden cases. This is probably because you’ve misunderstood the logic required when looking at the original cases, or have not handled all possible input possibilities, like passing a negative number or an empty array.
You can unlock hidden cases by completing more levels.
terimakasih banyak gan, sekarang bisa belajar dengan nyaman
sama-sama!