This is where I was headed. I wasn’t aware I could use arr[1][i] to look at each individual letter. For some reason I thought that indexOf would search the string for any number of combinations, but I was probably asking a bit much from the method. Thanks for the assist!
I ended up taking a round about way – initially tried converting the array to separate strings like the Basic Code Solution, but got stuck, so went back to keeping it as an array:
arr[0] = arr[0].toLowerCase();
arr[1] = arr[1].toLowerCase();
var result = [];
for (i=0; i<arr[1].length; i++) {
result[i] = arr[0].indexOf(arr[1][i]);
}
var resultArray = result.filter(function(val) {
return val===-1;
});
if (resultArray[0]===-1) {
return false;
}
return true;
}
I like this solution it is very similar to what I wanted to write but i do not understand why you put that “arr[1].length -1” why not just “arr[1].length”. I though I had understood for loops but these exercises have just shown me that what I learnt earlier does not even scratch the surface. when I see all these solutions, they seem to go againts everything i tought for loops should look like!! for loops are so complex and broad. I really dont like them. It seems like just when you think you get it, there is another BUT…
function mutation(arr) {
var a = arr[0].toLowerCase().split('');
var b = arr[1].toLowerCase().split('');
while (b.length) {
if (a.indexOf(b[0]) >= 0) {
b.splice(0, 1);
}
else if (a.indexOf(b[0]) < 0) {
return false;
}
}
return true;
}
This is a whacky thread. Here is yet another working solution. It might be more basic than the basic solution. It just counts the matches and returns true if the number of matches equals the length of the test string. Surprised I haven't seen anyone else use .includes(). It is the most literal method...indexOf being -1 never even occurred to me. I think I should get bonus nerd points for including c++ in my function....
function mutation(arr) {
var a = arr[0].toLowerCase();
var b = arr[1].toLowerCase().split('');
var c = 0;
for(var i = 0; i <= b.length; i++){
if(a.includes(b[i])){
c++;
}
}
return c === b.length ? true : false;
}
On a weird note the following works with pretty much anything in place of freeCodeCampRules....ternary syntax is weird
function mutation(arr) {
var a = arr[0].toLowerCase();
var b = arr[1].toLowerCase().split('');
var c = 0;
for(var i = 0; i<=b.length; i++){
freeCodeCampRules = a.includes(b[i]) ? c++ : null;
}
return c===b.length ? true : false;
}
function mutation(arr){
var d = arr[0].toLowerCase().split("");
var t = arr[1].toLowerCase().split("");
var w = [];
for (var j = 0; j < t.length; j++) {
w.push(d.indexOf(t[j]));
}
return ee = w.every(function(val){
return val !== -1
});
}
mutation(["hello", "hey"]);
function mutation(arr) {
for (var i = 0; i < arr[1].length; i++) {
if (arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) === -1) {
return false;
}
}
return true;
}
function mutation(arr) {
var wordToCompare = arr[1].toLowerCase();
var wordToCheck = arr[0].toLowerCase();
var countMatch = 0;
for(var i=0; i<wordToCompare.length; i++){
//ceck if the letter from second string match any letter from the first string
if(wordToCheck.indexOf(wordToCompare[i]) !== -1){
countMatch++;//count matches
}
}
//check if all the letter from the wordToCompare matched
if(countMatch === wordToCompare.length){
return true;
}
return false;
}
//test
mutation(["hello", "hey"]);
good point. thanks. when i do these type of problems i get very deliberate with my javascript…like var a,b,c, but in this case even i would understand what was going on if i cut out the ternary.
return c===b.length;
i will admit when i initially write the code i don’t use ternary syntax i use if/else. then when its time to show someone else i use ternary just because its the standard i guess. probably because i’m still noobish it takes me longer to make sense of ternary.
function mutation(arr) {
var g = 0;
var ans1 = arr[1].toLowerCase();
arr = arr[0].toLowerCase();
while (g < ans1.length) {
var alet = ans1[g];
var ans2 = arr.indexOf(alet);
if (ans2 == -1) {
return false;
}
g++;