Doubt regarding the existence of an "if" in the code

Asked

Viewed 48 times

0

Question statement: Construct a function that will receive two Strings of varying sizes and will return True or False if all characters (regardless of whether they are upper or lower case) are contained in both words.

Answer:

function verificacaoDeString (string1, string2) {
    let estaContido = true;
    for (let i = 0; i < string1.length; i++) {
        let caractereString1 = string1.charAt(i).toLowerCase()
        for(let j = 0; j < string2.length; j++){
            let caractereString2 = string2.charAt(j).toLowerCase()
            if(caractereString1 == caractereString2) {
                estaContido = true
                break
            } else {
                estaContido = false
            }
        }
        if(!estaContido) {
            return estaContido
        }
    }
    return estaContido
 } 

My doubt is the question of the existence of this passage :

if(!estaContido) {
            return estaContido
        } 

I apologize for the ignorance, but I also ask for your understanding that I am still a beginner in the developing world.

  • That sounds like an "Early-Return". That is, if the for internal fail (note, there are two for), then there is no reason to continue iterating. This probably can also be exchanged for if(!estaContido) { break }, since in the end you already give a return estaContido. But, finally, both fulfill the same purpose: to stop the loop in the first error, instead of continuing.

1 answer

2

There are two for, one inside the other:

   // For do `string1`
   for (let i = 0; i < string1.length; i++) {
        let caractereString1 = string1.charAt(i).toLowerCase()

        // For do `string2`
        for(let j = 0; j < string2.length; j++){
            let caractereString2 = string2.charAt(j).toLowerCase()
            if(caractereString1 == caractereString2) {
                estaContido = true
                 
                // Se estiver contido: interrompe o "For do string2"
                break
            } else {
                estaContido = false
            }
        }
        
        // Se não estiver contido, não há porque continuar com o "For do string1".
        if(!estaContido) {
            // O return retornará a função imediatamente.
            return estaContido
        }
   }

For each letter of string1 will run a new for, this being the "For of String2". "For String2" defines it as "estaContido = false" if there is no letter and defines it as "estaContido = true" if there is one and also "break" to interrupt "For String2" (but it will still run "For String1").

If the result is "estaContido = false", there is no reason why the "string1" is still running, since a letter has already failed.

The question you mention explains that: "True or False if all characters". If a character is already different, why will you continue? If one is different, it returns immediately. ;)


I ignored the fact that "Answer" did not do what you asked (it only validates string1 against string2, not the reverse). I restricted myself in trying to explain such if. But... I don’t use much Javascript, but parently you can use the Set, pass to an Array, use Sort and then compare the two as string:

function verificacaoDeString (string1, string2) {
return [...new Set([...string1])].sort().join("") === [...new Set([...string2])].sort().join("");
}

That settles that question, apparently.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.