D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Jn1532
Full window
Github gist
JS Bin anagram checker and reduce example // source http://jsbin.com/felila
<!DOCTYPE html> <html> <head> <meta name="description" content="anagram checker and reduce example"> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <script id="jsbin-javascript"> var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']; var countedNames = names.reduce(function (allNames, name) { if (name in allNames) { allNames[name]++; } else { allNames[name] = 1; } return allNames; }, {}); console.clear(); console.log(countedNames); // countedNames is: // { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 } var word1 = "aba"; var word2 = "aa"; // convert strings to LC for case insensitivity // split strings into arrays // sort the arrays (spaces will sort first and be trimmed later) // join the arrays back into strings // we're only concerned about the printable characters in the anagram so, // trim() to remove any spaces and then compare the resulting strings var str1 = this.word1.toLowerCase().split('').sort().join('').trim(); var str2 = this.word2.toLowerCase().split('').sort().join('').trim(); if (str1 === str2) { console.log("true"); this.isAnagram = true; } else { console.log("fff"); this.isAnagram = false; } </script> <script id="jsbin-source-javascript" type="text/javascript"> var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']; var countedNames = names.reduce(function (allNames, name) { if (name in allNames) { allNames[name]++; } else { allNames[name] = 1; } return allNames; }, {}); console.clear(); console.log(countedNames); // countedNames is: // { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 } var word1 = "aba"; var word2 = "aa"; // convert strings to LC for case insensitivity // split strings into arrays // sort the arrays (spaces will sort first and be trimmed later) // join the arrays back into strings // we're only concerned about the printable characters in the anagram so, // trim() to remove any spaces and then compare the resulting strings var str1 = this.word1.toLowerCase().split('').sort().join('').trim(); var str2 = this.word2.toLowerCase().split('').sort().join('').trim(); if (str1 === str2) { console.log("true"); this.isAnagram = true; } else { console.log("fff"); this.isAnagram = false; }</script></body> </html>