There are similar questions on the net but I haven't been able to find something specific that works for my problem (or if it can be done).
I have a function that has to do several things but if the first letter of (str)
is a vowel, nothing else has to be done than add "way" to the(str)
function translatePigLatin(str) {
var vocales = ["a","e","i","o","u"];
var recogedorConsonantes = []
var contadorLetrasQuitadas = 0;
var strVocal = "way";
//SI el IF stament dentro de este bucle se cumple,
// quiero que todo lo demás de abajo no se ejecute
for (var i = 0; i < vocales.length; i++){
if (str[0] === vocales[i]){
str = str.concat(strVocal)
}
else {1==1}}
// A PARTIR DE AQUÍ detener script si el if de arriba se ha cumplido
/*for (letra in str){
if (str[letra]){
}
else if (-------){
}
}*/
return str.slice(contadorLetrasQuitadas, )
}
console.log(translatePigLatin("california"));
console.log(translatePigLatin("glove"));
console.log(translatePigLatin("eight")) // Con esta debería pararse
it str.slice(contadorLetrasQuitadas, )
is not necessary to cancel it since if the first if is fulfilled nothing will be added to contadorLetrasQuitadas
which will not modify anything of(str)
I have tried to put break, and "return false" in places where they say but it does not work.
break
what it does is "break" a loop. That is, it exits the loop. If you're in afor
, and you want to end it early, then you put abreak
.return
what it does is "return" a value. That is, it exits a function. If you're in a function, wherever you want it to end, you put areturn
. You can accompany it with some value or variable so that it is returned.You also have
continue
, which in this case will not work for you, but I'll explain it to you anyway. What it doescontinue
is terminate the current iteration of a loop. That is, if you are in afor
or awhile
, if you put acontinue
in the middle of the function, when the is reachedcontinue
it will skip to the next iteration of the loop. That is, it starts at the top with the next value.Knowing what each thing does, you can use what suits you at all times. In your case, for what you describe you want a
return
, that is, something that exits the function directly. You may want to make areturn str;
so that it returns the valuestr
.The
break
would just end the loop, but it will still be inside the function.I have tried your code, with
break
it does not stop, but trying withreturn false
, it does what you need.Look at this adapted example: