I feel like that all the time. Check out the solutions at the bottom of the “hints” article to glean some inspiration for more concise approaches: freeCodeCamp Challenge Guide: Mutations
I’ve just been experimenting with this one and got stuck for a while, but eventually solved it with some quite simple code by leaning on the functional array features:
function mutation(arr) {
let target = [...arr[0].toLowerCase()];
let source = [...arr[1].toLowerCase()];
return source.every(item => (target.indexOf(item) > -1));
}
mutation(["hello", "hey"]);
Use the [...arr[0]] spread operator to get split the string into an array, then every to ensure that all must match for it to pass.
But the difference between the two are two different approaches, so its not like one is better or worse. I’ve written it in a functional way and you’ve used an imperative approach:
I did the imperative patterns for a long time, and now im relearning / rewiring myself to try to think in more functional terms.
Edit: Although, looking at yours , there are a few spots for improvement, like you can just use a bool directly here:
let notAllPresent = 0;
for (let k = 0; k < flagArray.length; k++) {
if (flagArray[k] == 0) {
notAllPresent = 1;
break;
}
}
if (notAllPresent) {
return false;
} else {
return true;
}
could be
let notAllPresent = false;
for (let k = 0; k < flagArray.length; k++) {
if (flagArray[k] == 0) {
notAllPresent = true;
break;
}
}
return notAllPresent;
It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.
You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.