mercredi 6 mai 2020

Can someone explain the relationship between stringToCount and [characterPosition] using this example?

Im a beginner going through a Javascript tutorial before doing a Java Bootcamp(wish me luck!).

Using this code, can someone explain to me the relationship between stringToCount and [characterPosition] in the if statement?

I understand everything else about this code, but I'm struggling to grasp how this part reads. It looks like its 'and' but that would be &&. With no operands between them, I just need to understand this line: if (stringToCount[characterPosition] == characterToFind).

Thus far in my studies, I only experienced brackets[ ] being used for arrays. Is characterPosition considered an array after the loop gets created? The tutorial I'm using (Software Guild) doesn't break this detail down and I need to fully understand it prior to moving forward.

Thanks in advance.

function countingCharacters2(stringToCount, characterToFind){
    // Let's count the number of times a character appears in a string
    // We will look at each character one-by-one with the help of a for loop
    var characterCount = 0;
    for (var characterPosition = 0; 
           characterPosition < stringToCount.length; 
           characterPosition++){
        if (stringToCount[characterPosition] == characterToFind){
            characterCount++;
        }
    }
    console.log("String to search in: " + stringToCount);
    console.log("Character to find: " + characterToFind);
    console.log("Number of times the character appears: " + 
                  characterCount);
}

Aucun commentaire:

Enregistrer un commentaire