I am passing all test cases instead of the first one. Any ideas please?
**Your code so far**
function mutation(arr) {
// arr[0]
let firstElement = arr[0].toLowerCase();
// arr[1]
let splitArr = arr[1].toLowerCase().split("");
// Just so I could see what was going on.
console.log(splitArr, arr[0]);
// Iterating through the test array(2nd element).
for(let i = 0; i < splitArr.length; i++) {
if(firstElement.indexOf(splitArr[i]) <= -1){
return false;
}
else {
return true;
}
}
}
mutation(["hello", "hey"]);
function mutation(arr) {
// arr[0]
let firstElement = arr[0].toLowerCase();
// arr[1]
let splitArr = arr[1].toLowerCase().split("");
// Just so I could see what was going on.
console.log(splitArr, arr[0]);
// Iterating through the test array(2nd element).
for(let i = 0; i < splitArr.length; i++) {
if(firstElement.indexOf(splitArr[i]) <= -1){
return false;
}
else {
return true;
}
}
}
mutation(["hello", "hey"]);
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0.
Thanks, I assumed it was something along those lines but wasn’t 100%
Just so I am clear,
if(condition) {
console.log('true');
}
return console.log('condition failed');
// So the above is the equivilent of having the following correct?
if(condition) {
console.log('true');
}
else {
return console.log('condition failed');
}
// Or would it need to be inside the if statements {} like so:
if(condition) {
console.log('true');
return console.log('condition failed');
}
no, because in the first case if condition is true, both inside and outside of the if statement get executed, in the second case only the inside of the if statement
what behaves the same is when you have a return statement inside the if statement., as if it executes the code stop there
The return is important here. A return exits the function, so nothing happens after it. If your if block does not have a return, then using an else makes a difference.
(You cannot meaningfully return a console statement. It will return undefined.)
If condition is truthy then it will print “true” AND hit the return. If condition is falsey, it will only hit the return.
if condition is truthy, it will print “true” and not hit the return. If it’s falsey, it will hit the return.
If condition is truthy it will print “truthy” AND hit the return. If it is falsey, it will do neither.
Cheers, I’m gonna have a little play around on this site you’ve suggested, I feel like an idiot, I am looking at it and I feel like my subconscious understands but I don’t lol.