Created
March 8, 2017 20:40
-
-
Save Jn1532/17113896bedad87d9db402f35a503cb5 to your computer and use it in GitHub Desktop.
JS Bin character frequency part 2 // source http://jsbin.com/kaduruy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="character frequency part 2"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var maxSpeed = { | |
car: 300, | |
bike: 60, | |
motorbike: 200, | |
airplane: 1000, | |
helicopter: 400, | |
rocket: 8 * 60 * 60 | |
}; | |
function getLetterCount(arr){ | |
return arr.reduce(function(prev,next) | |
{ | |
prev[next] = (prev[next] + 1) || 1; | |
return prev; | |
},{}); | |
} | |
var xx ="hayjf".split('') ; | |
console.clear(); | |
console.log(getLetterCount(xx)); | |
/////////////////////////////////////////////////////////////////////// | |
var sortable = []; | |
for (var vehicle in maxSpeed) { | |
console.log(vehicle+ ' ' + maxSpeed[vehicle]); | |
sortable.push([vehicle, maxSpeed[vehicle]]); | |
} | |
sortable.sort(function(a, b) { | |
return a[1] - b[1]; | |
}); | |
function compareNumbers(a, b) { | |
return a[1] - b[1]; | |
} | |
console.log('Sorted without a compare function:', sortable.sort()); | |
console.log('Sorted with compareNumbers:', sortable.sort(compareNumbers)); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var maxSpeed = { | |
car: 300, | |
bike: 60, | |
motorbike: 200, | |
airplane: 1000, | |
helicopter: 400, | |
rocket: 8 * 60 * 60 | |
}; | |
function getLetterCount(arr){ | |
return arr.reduce(function(prev,next) | |
{ | |
prev[next] = (prev[next] + 1) || 1; | |
return prev; | |
},{}); | |
} | |
var xx ="hayjf".split('') ; | |
console.clear(); | |
console.log(getLetterCount(xx)); | |
/////////////////////////////////////////////////////////////////////// | |
var sortable = []; | |
for (var vehicle in maxSpeed) { | |
console.log(vehicle+ ' ' + maxSpeed[vehicle]); | |
sortable.push([vehicle, maxSpeed[vehicle]]); | |
} | |
sortable.sort(function(a, b) { | |
return a[1] - b[1]; | |
}); | |
function compareNumbers(a, b) { | |
return a[1] - b[1]; | |
} | |
console.log('Sorted without a compare function:', sortable.sort()); | |
console.log('Sorted with compareNumbers:', sortable.sort(compareNumbers));</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var maxSpeed = { | |
car: 300, | |
bike: 60, | |
motorbike: 200, | |
airplane: 1000, | |
helicopter: 400, | |
rocket: 8 * 60 * 60 | |
}; | |
function getLetterCount(arr){ | |
return arr.reduce(function(prev,next) | |
{ | |
prev[next] = (prev[next] + 1) || 1; | |
return prev; | |
},{}); | |
} | |
var xx ="hayjf".split('') ; | |
console.clear(); | |
console.log(getLetterCount(xx)); | |
/////////////////////////////////////////////////////////////////////// | |
var sortable = []; | |
for (var vehicle in maxSpeed) { | |
console.log(vehicle+ ' ' + maxSpeed[vehicle]); | |
sortable.push([vehicle, maxSpeed[vehicle]]); | |
} | |
sortable.sort(function(a, b) { | |
return a[1] - b[1]; | |
}); | |
function compareNumbers(a, b) { | |
return a[1] - b[1]; | |
} | |
console.log('Sorted without a compare function:', sortable.sort()); | |
console.log('Sorted with compareNumbers:', sortable.sort(compareNumbers)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment