Need help with learning to read a javascript

So i was looking around and i found this

for (var i = 0; i < $targetCardPlayer.hand.length; i++) {
var card = $targetCardPlayer.hand;
$calc.list.push(card);
}

This is a code for a card game and that it shows me the opponent's hand. So I'm wondering how to read this javascript.
  • I know var i =0 which is less than the opponent's hand and repeat for i++ for every other card in the opponent's hand.
But I do not know how

var card = $targetCardPlayer.hand;
$calc.list.push(card);

is read.
I'm guessing that var card is to store the current card that the player has, then stores (pushing it) to the list inside $calc variable. Possibly this $calc is used somewhere else to perform... damage calculation?

$targetCardPlayer.hand is a list of some sort to store the active cards.

And it will do this process until the for loop finishes reading every card the target player is holding at hand. ((Correct meh if I'm wrong orz))
I'm guessing that var card is to store the current card that the player has, then stores (pushing it) to the list inside $calc variable. Possibly this $calc is used somewhere else to perform... damage calculation?

$targetCardPlayer.hand is a list of some sort to store the active cards.

And it will do this process until the for loop finishes reading every card the target player is holding at hand. ((Correct meh if I'm wrong orz))

Hmmmm could be. Gonna learn to read javascript and other people custom javascript oh dear me XD
@marsatche is correct~
  • for (var i = 0; i < $targetCardPlayer.hand.length; i++)
    A for loop: repeat the following {block} of code.
    1. Define a variable i that starts with a value of 0.
    2. Before running the code, check if i is less than the number of items in the array named $targetCardPlayer.hand: if it isn't, skip the code block and end the loop.
    3. After running the code, increase the value of i by 1 and return to step 2.

  • var card = $targetCardPlayer.hand[i];
    Define a new variable called card, and set it equal to the ith item in $targetCardPlayer.hand.

  • $calc.list.push(card);
    Add card as a new entry to the end of the array stored in the list property of the $card object.
These lines all involve Arrays, see here for more details: Array (MDN).

If you don't know what I mean by words like "object" or "property", then maybe hunt around for tutorials on the basics of object-oriented programming ("OOP"): classes, objects, properties, methods, inheritance, etc. JavaScript uses a slightly different structure to many OOP languages...you can read up on the details here, but it may not make much sense if you're a total beginner. 😅


Also, about that disappearing [i] in your post: [i] is interpreted as a post-formatting tag here (and on many forums). To avoid this, you can use a code block, either by using the Insert > Code button from the toolbar (just above the textbox where you type out your post) or by writing it out like [code]This is code[/code]; it will appear like this:
Code:
for (var i = 0; i < $targetCardPlayer.hand.length; i++) {
  var card = $targetCardPlayer.hand[i];
  $calc.list.push(card);
}
😤
@marsatche is correct~
  • for (var i = 0; i < $targetCardPlayer.hand.length; i++)
    A for loop: repeat the following {block}of code.
    1. Define a variable i that starts with a value of 0.
    2. Before running the code, check if i is less than the number of items in the array named $targetCardPlayer.hand: if it isn't, skip the code block and end the loop.
    3. After running the code, increase the value of i by 1 and return to step 2.

  • var card = $targetCardPlayer.hand[i];
    Define a new variable called card, and set it equal to the ith item in $targetCardPlayer.hand.

  • $calc.list.push(card);
    Add card as a new entry to the end of the array stored in the list property of the $card object.
These lines all involve Arrays, see here for more details: Array (MDN).

If you don't know what I mean by words like "object" or "property", then maybe hunt around for tutorials on the basics of object-oriented programming ("OOP"): classes, objects, properties, methods, inheritance, etc. JavaScript uses a slightly different structure to many OOP languages...you can read up on the details here, but it may not make much sense if you're a total beginner. 😅


Also, about that disappearing [i] in your post: [i] is interpreted as a post-formatting tag here (and on many forums). To avoid this, you can use a code block, either by using the Insert > Code button from the toolbar (just above the textbox where you type out your post) or by writing it out like [code]This is code[/code]; it will appear like this:
Code:
for (var i = 0; i < $targetCardPlayer.hand.length; i++) {
  var card = $targetCardPlayer.hand[i];
  $calc.list.push(card);
}
😤

Ah, thanks for the help this post has been solved! 🙂

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/need-help-with-learning-to-read-a-javascript.117917/