# Chingu Challenge #1 on HackerRank

I am trying to gain as much JavaScript ‘experience’ as I can in prep for [hopefully] being invited to join one of @tropicalchancer’s cohorts. I’ve been trying to work through the 1st challenge and for the life of me I just can’t seem to get through it … yet.

I understand the example just fine, but when it gets to the editor with all the parseInts and readLines, I’m totally confused. I also do not understand the expression in the for loop. The 'a0’s don’t make any sense to me at all.

I’m not looking for the solution … I’m looking for help on how to be able to solve it myself … I’m bound and determined that this ole dog can learn new tricks, and I will solve this thing one way or another … in the meantime I’m wearing a permanent dent in my forehead from banging it on my desk this past week!

Here’s the code,

``````function main() {
for (var a0 = 0; a0 <t, a0++) {
}
}
``````

I have tried a few other coding places as well, but with the similar results of not even being able to get out of the starting gate … very frustrating!

Thankfully, I have managed to get through the first two challenges at codeabbey.com! I was beginning to think I’d never get this.

Can you link to the actual challenge? I’m not sure what it is that the code is supposed to do.

I can say - and it may or may not help - that `n` is being given a new value every time you pass through the for-loop, and that the previous values of `n` are being “overwritten”.

Yes, thank you … here is the link to the challenge
https://www.hackerrank.com/contests/chingu-startup/challenges/euler001

Thank you so much for offering to help! Greatly appreciated!

Okay… so the `readLine` and `parseInt` are both functions. Let’s walk through just the first line of code inside of ‘main’, and see what it does:

1. The computer sees “var t”. It finds a box, grabs a sharpie, and writes `t` on the outside of the box.

2. The computer skips over the equals sign, and sees a function called `readLine`, followed by `()`. The computer calls the function `readLine`, and “returns” (i.e. spits out) a value.

3. That computer takes that value - whatever it is - and hands it over to the function `parseInt`. The function `parseInt` takes that value, turns it into a number, and returns that number.

4. Now you’re back to the equals sign. The computer takes the value on the right side of the equals sign - the number - and stores it in the box that it created back in step 1.

Make sense so far?

Aye … that makes great sense so far! THANK YOU! but question … are parseInt and readLine native JavaScript functions or ???

`parseInt` is - see here for an explanation and some examples.

`readLine` is not. It’s defined by HackerRank (the definition is actually right above the line that says to “ignore above this line”.

Okay … I didn’t understand the code above the line, but that’s okay… so let me see if I understand something … it took me several minutes but I think what you’re telling me is that for var declarations I start from the right hand side and evaluate to the left at which time the evaluation will then be assigned to the variable name on the left … I guess I “knew” that, but didn’t really ‘see’ it, if that makes sense.

The other thing is the for loop … what is the ‘a0’? I expect an a [or i or some other variable name] but the a0’ is throwing me off.

Again, thank you so very much for taking the time to explain this to me!

a0 is just the variable name, and an odd one at that. You could easily rewrite this like so:

``` var total = parseInt(readLine()); for ( var counter=0; counter < total; count+=1){ var currentNumber = parseInt(readLine()); }```

`a0` is just a variable name. You could just as well call it `a` or `i` anything else. The very first time you enter the for-loop, the computer will create a variable (in this case, `a0`). The value of `a0` is (in this case) zero.

Then the computer move over to the next “section”, and check to make sure that `a0` is less than `t`. If it is, then you’ll jump down inside the loop and follow whatever instructions are given there (in this case, `var n = parseInt(readLine())`).

Once that’s done, the computer moves on to the last part of the “instructions” at the top of the for-loop, the `a0++`. (The `a0++` is just an abbreviation for `a0 = a0 + 1`.). It then rechecks that `a0` is still less than `t`, etc. etc.

If you want to play around a bit, go ahead and type in `console.log(n)` inside the for-loop (after the `var n = ...`, and then run the code. It should print out the second and third “lines” of input (10 and 100).

If you check the box that says “test against custom input”, you can try putting in different numbers, and see what happens. That’s a good way to get a feel for what the code is actually doing.

Thank you @AgathaLynn and @Josh5231! I think I have enough information now to work through the challenge.

Again, I greatly appreciate it!

I followed the link and it came to a 404 page. But based on what you just wrote here as an example.

``````var t = parseInt( readLine( ) ) // Reads something from input, assuming it is a number.  Otherwise it will crash.
for( var a0 = 0; a0 < t; a0++ ) // uses the input t from the first statement as end condition for the for loop.
``````

parseInt( ) is a function / method (some other language uses different terminology)
before parseInt( ) can be resolve, it must resolve the readline( ) function.

When readline( ) resolves by the user entering an input.
parseInt( ) will attempt to resolve the input that was entered through readline( ).
The reason why it needs to do this, because the stuff entered through readline( ) is a string.

If I entered the number 8792, it reads “8792” as string not a number.
parseInt( ) will turn that string “8792” into a number so that it can be use inside the for loop.

Another words, if the variable t is read from the user the number 7
The loop that comes will read like this.

``````for( var a0 = 0; a0 < 7; a0++ ) {
var n = parseInt( readline( ));
}
``````

“a0” is the variable which is use as the counter for the loop. It is used as the left operand in the condition against the variable t.

Thanks @Cowwy! Appreciate the input … that helps a great deal too!

After sleeping on these explanations and coming back to it this morning, this explanation about readLine’s input being a string and thus requiring the parseInt function to convert it to a number was EXTREMELY helpful! This is starting to make a lot more sense to me now … hopefully, I’ll have this figured out by the end of the day!

So very appreciative for all the explanations and help!

2 Likes