Ad
Code
Diff
  • const isAnagram = (s1, s2) => s1.length === s2.length && Object.values([...s1].reduce((o, k, i) => (k in o ? ++o[k] : o[k] = 1, s2[i] in o ? --o[s2[i]] : o[s2[i]] = -1, o), {})).every(v => !v);
    • function isAnagram(str1, str2) {
    • if (str1.length !== str2.length) {
    • return false;
    • }
    • const frequency = {};
    • for (let i = 0; i < str1.length; i++) {
    • const char = str1[i];
    • if (frequency[char]) {
    • frequency[char]++;
    • } else {
    • frequency[char] = 1;
    • }
    • }
    • for (let i = 0; i < str2.length; i++) {
    • const char = str2[i];
    • if (!frequency[char]) {
    • return false;
    • } else {
    • frequency[char]--;
    • }
    • }
    • return true;
    • }
    • const isAnagram = (s1, s2) => s1.length === s2.length && Object.values([...s1].reduce((o, k, i) => (k in o ? ++o[k] : o[k] = 1, s2[i] in o ? --o[s2[i]] : o[s2[i]] = -1, o), {})).every(v => !v);
Code
Diff
  • const reverse = str => [...str].reverse();
    • const reverse = str => str.split('').reverse();
    • const reverse = str => [...str].reverse();
Code
Diff
  • function capitalize(sentence) {
      return sentence.split(' ').map(w => w[0].toUpperCase() + w.slice(1)).join(' ');
    }
    • function capitalize(sentence) {
    • let words = sentence.split(' ');
    • const length = words.length;
    • for (let i = 0; i < length; ++i) {
    • words[i] = words[i][0].toUpperCase() + words[i].substr(1)
    • }
    • return words.join(' ');
    • }
    • console.log(capitalize("i am a javascript programmer"));
    • return sentence.split(' ').map(w => w[0].toUpperCase() + w.slice(1)).join(' ');
    • }
Code
Diff
  • const numMinusSeven = num => Math.ceil(num / 7);
    • function numMinusSeven(num) {
    • let aaa = []
    • while (num > 0) {
    • num -= 7
    • aaa.push(num)
    • }
    • return aaa.length
    • }
    • const numMinusSeven = num => Math.ceil(num / 7);
Code
Diff
  • const addArr = arr => arr.length ? arr.reduce((sum, num) => sum + num, 0) : null;
    • function addArr(arr){
    • if(arr.length === 0) return null
    • let final = 0
    • arr.forEach(num => {
    • final += num
    • })
    • return final
    • }
    • const addArr = arr => arr.length ? arr.reduce((sum, num) => sum + num, 0) : null;