Bonfire Mutations - GJSmith3rd/FreeCodeCamp-BootCamp GitHub Wiki
Contact me
Gilbert Joseph Smith III
Github | FreeCodeCamp | CodePen | LinkedIn | Blog/Site | E-Mail
Details
- Difficulty: 1/5
Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
For example, ['hello', 'Hello'], should return true because all of the letters in the second string are present in the first, ignoring case.
The arguments ['hello', 'hey'] should return false because the string 'hello' does not contain a 'y'.
Lastly, ['Alien', 'line'], should return true because all of the letters in 'line' are present in 'Alien'.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Useful Links
Problem Script:
function mutation(arr) {
return arr;
}
mutation(['hello', 'hey']);
Explanation:
You basically have to check if the string in the first parameter contains all the characters found on the second parameter.
Hint: 1
First make the arguments lowercase and arrays so we can compare without having to worry about the case.
Hint: 2
Use inndexOf() to check if the letter of the second word is on the first.
Hint: 3
You will need a way to check if all the words are in the first one, like keeping a counter and checking for a default value.
My code:
function mutation(arr) {
var first = arr[0].toLowerCase().split('');
var second = arr[1].toLowerCase().split('');
var temp = 0;
// Check every character and if the index is found add one
for (var s in second){
if (first.indexOf(second[s]) > -1) {
temp+= 0;
} else
temp+=1;
}
if (temp === 0)
return true;
else
return false;
}
My Code Explanation:
- take the first and second parameter and make them lowercase and split them by characters.
- Create a temp variable to keep count.
- For each letter in the second parameter, check if it is found on the first, if so then add 0 to the tamp variable, otherwise add 1.
- Outside the loop, if temp is 0 then return true, otherwise false, this acts as an on and off. If all the letters were added then we just get 0, otherwise there will be at least a
1which means one letter was not found.