Attempt 1 →

function strToArr(str) {
  let arr = [];
  for (let i = 0; i < str.length; i++) {
    arr.push(str[i]);
  }
  return arr;
};

function countFrequency(arr) {
  let map = {};
  arr.forEach(function(item) {
      if (map[item] === undefined) {
        map[item] = 1;
      }
      else {
        map[item]++;
      }
    });
  return map;
};

function isAnagram(str1, str2) {
  let flag = 0;

  let arr1 = strToArr(str1);
  let arr2 = strToArr(str2);

  let map1 = countFrequency(arr1);
  let map2 = countFrequency(arr2);

	arr1.forEach(function(item) {
    if(map2[item] != map1[item]) {
        flag++;
      })
  
    if (flag != 0) {
      return false;
    }
  
    return true;
  }
  
}

module.exports = isAnagram;

Problems →

  1. Failing for test cases where there’s more letters than the common ones.
// hello
// hello!

// still returning true

Attempt 2 →

function strToArr(str) {
  strLower = str.toLowerCase();
  let arr = [];
  for (let i = 0; i < strLower.length; i++) {
    arr.push(strLower[i]);
  }
  return arr;
};

function countFrequency(arr) {
  let map = {};
  arr.forEach(function(item) {
      if (map[item] === undefined) {
        map[item] = 1;
      }
      else {
        map[item]++;
      }
    });
  return map;
};

function isAnagram(str1, str2) {
  let flag = 0;

  let arr1 = strToArr(str1);
  let arr2 = strToArr(str2);

  let map1 = countFrequency(arr1);
  let map2 = countFrequency(arr2);

  if (arr1.length === arr2.length) {
    arr1.forEach(function(item) {
      if(map2[item] != map1[item]) {
        flag++;
      }
    })
  
    if (flag != 0) {
      return false;
    }
  
    return true;
  }
  else {
    return false;
  }
  
}

Problems →

  1. Failing for test cases with casing
// Debit card
// debit Card

Final Attempt →

/*
  Write a function `isAnagram` which takes 2 parameters and returns true/false if those are anagrams or not.
  What's Anagram?
  - A word, phrase, or name formed by rearranging the letters of another, such as spar, formed from rasp.
*/

function strToArr(str) {
  strLower = str.toLowerCase();
  let arr = [];
  for (let i = 0; i < strLower.length; i++) {
    arr.push(strLower[i]);
  }
  return arr;
};

function countFrequency(arr) {
  let map = {};
  arr.forEach(function(item) {
      if (map[item] === undefined) {
        map[item] = 1;
      }
      else {
        map[item]++;
      }
    });
  return map;
};

function isAnagram(str1, str2) {
  let flag = 0;

  let arr1 = strToArr(str1);
  let arr2 = strToArr(str2);

  let map1 = countFrequency(arr1);
  let map2 = countFrequency(arr2);

  if (arr1.length === arr2.length) {
    arr1.forEach(function(item) {
      if(map2[item] != map1[item]) {
        flag++;
      }
    })
  
    if (flag != 0) {
      return false;
    }
  
    return true;
  }
  else {
    return false;
  }
  
}

module.exports = isAnagram;