lundi 22 juillet 2019

How to reduce character count in If statement utilizing ||

I want to know if there is a way to shorten an if statement for counting the number of specific characters in an array.

This is for a problem I'm working out in my online schooling.

I wanted to use

`if (array[i] === ('.' || '!')`

But I think it is evaluating the first statement as true and skipping the second because of this, which doesn't make sense to me as there are '!' characters. I thought that would mean array[i] !== '.' but does equal '!' and so would still count as if being evaluated as true.

My working code is:

let array = [<12 sentences, 9 end in '.' and 3 end in '!'>]

let sentences = 0

for (let i = 0; i < array.length; i++) {
    if (array[i] === '.' || array[i] === '!') {
        sentences += 1
    };
};

This outputs correctly, counting the number of '.' and '!' characters, but I want to decrease the character count on the if condition.

Aucun commentaire:

Enregistrer un commentaire