Skip to content

Instantly share code, notes, and snippets.

@Kaeyz
Created July 7, 2018 10:11
Show Gist options
  • Save Kaeyz/36829da7fa16549a51e46aca0f53bde7 to your computer and use it in GitHub Desktop.
Save Kaeyz/36829da7fa16549a51e46aca0f53bde7 to your computer and use it in GitHub Desktop.
anagram: to test if a word is an anagram or not
function anagram(str) {
let arr = str.split('');
let anaResult, anaValue, response;
let finalArr = [];
for (let index = 0; index < arr.length; index++) {
let cleanNewArray = [];
let result = true
let resultArr = [];
let compare = arr[index];
for (let index = 0; index < arr.length; index++) {
if (compare === arr[index]) {
result = true
} else {
result = false
}
resultArr.push(result)
}
for (let index = 0; index < resultArr.length; index++) {
cleanNewArray = resultArr.filter(function (cleanArray) {
return cleanArray === true
})
if (cleanNewArray.length > 1) {
anaResult = false;
} else {
anaResult = true
}
};
finalArr.push(anaResult);
}
anaValue = finalArr.filter(function (k) {
return k !== true
})
if (anaValue.length !== 0) {
response = false;
} else {
response = true;
}
console.log(response);
return response;
}
anagram('abcdefghijklmnopqrstuvwxyz');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment