@P1xt I think it’s possible to use Regular Expressions with a similar approach to the “did any of these not match”
function mutation(arr) {
for (var i = 0; i < arr[1].length; i++){
var pattern = arr[1][i];
var re = new RegExp(pattern, "gi");
if (!re.test(arr[0]))
return false;
}
return true;
}
Although this solution looks very clumsy, it was very important and useful for me to solve this problem by myself and gain rich experience on how each method works (and how my brain works too). After I solved this problem I’ve found in this topic the best solution and study it carefully. I try to avoid all unnecessary steps in my code in future. Thanks to everybody in this topic!!!
function mutation(arr) {
var arr1 = arr.slice(0,1);
var arr2 = arr.splice(1);
var arrToString1 = arr1.toString(’’);
var arrToString2 = arr2.toString(’’);
function mutation(arr) {
// Iterate through every character in the second string, to check if it is present in the first string
for (var i=0; i<arr[1].length; i++) {
// Convert all characters to lower case
if ( arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) == -1 ) {
return false;
}
}
return true;
}
mutation(["Mary", "Army"]);
function mutation(arr) {
var x = arr[0].toLowerCase();
var y = arr[1].toLowerCase().split("");
var matches = 0;
for (var i = 0; i < y.length; i++){
if (x.match(y[i])){
matches++;
}
}
if (matches === y.length){
return true;
}
else return false;
}
mutation(["Hello", "heyn"]);
Here’s my rather convoluted solution — but with only things learned from earlier exercices. Does it make any sense doing this ?
function mutation(arr) {
// Preparing the array so words like "Hello" become "helo",
// and "VoOdoO" become "vodo".
// in order to get unique letters, and get rid of duplicates
var preparedArray = arr.map(function(val){
return val
.toLowerCase()
.split('') // to process individual letters
.reduce(function(result, current){
var len = result.length;
// rebuild the word with only unique letters
// if the current letter is already in our string, pass
// else append it to the word.
while (len){
if (result[len - 1] === current)
return result;
len--;
}
return result + current;
}, "");
});
// Get minimum required letters by getting
// the length of the shortest word in the array
var letters = Math.min(preparedArray[0].length, preparedArray[1].length);
// Comparing the letters in both words,
// decrementing the minimum letter requirement
for (var i = 0; i < preparedArray[0].length; i++){
for (var j = 0; j < preparedArray[1].length; j++){
if (preparedArray[0][i] === preparedArray[1][j])
letters--;
}
}
// If the minimum is reached, return "true"
return letters === 0;
}
Who r u? how u practice?How u write this better code?I am new here and also new to coding ,that is why i am asking.My codes r not this clear and efficient as Yours.do u practice day and night.
function mutation(arr) {
var a = arr[0].toLowerCase().split('');
var b = arr[1].toLowerCase().split('');
return b.every(function (element) {
return a.indexOf(element) > -1;
});
}
Maybe I’m a bit more experienced than you
I think first time my code was awful but 10 years of practice… and practice… and practice… (I’m java-dev) and now it’s much better I hope.
So, just learn and practice and you’ll manage, belive me.
Please put in ur comments :slight_smile:
function mutation(arr) {
for(var i=0;i<arr[1].length;i++){
if(arr[0].toLowerCase().indexOf(arr[1].toLowerCase().charAt(i))==-1)
return false;
}
return true;
}