Skip to content

Instantly share code, notes, and snippets.

Created November 23, 2015 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/7f878720c5dbf7706dea to your computer and use it in GitHub Desktop.
Save anonymous/7f878720c5dbf7706dea to your computer and use it in GitHub Desktop.
JS Bin Exercise Blank // source http://jsbin.com/mecububepi
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script>
<meta name="description" content="Exercise Blank">
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// 1. Implement a map() method, which creates a new array with the results of calling a provided function on every element in this array.
Array.prototype.map = function (func) {
var arr = [];
for(var i=0; i<=this.length-1;i++){
arr[i] = func(this[i]);
}
return arr;}
JSON.stringify([1,2,3].map(function(x) { return x + 1; })) === '[2, 3, 4]';
//console.log(JSON.stringify([1,2,3].map(function(x) { return x + 1; })));
// 2. Implement filter(). Add a filter() function to the Array type. The filter() function accepts a predicate. A predicate is a function that accepts an item in the array, and returns a boolean indicating whether the item should be retained in the new array.
//...
Array.prototype.filter = function(func) {
var arr = [];
for(var i=0; i<=this.length-1;i++){
if(func(this[i])){
arr.push(this[i]);
}
}
return arr;
}
JSON.stringify([1,2,3].filter(function(x) { return x > 2})) === '[3]';
//console.log(JSON.stringify([1,2,3].filter(function(x) { return x > 2})));
// 3. Compute the factors of a positive integer. A factor is a whole number which divides exactly into a whole number, leaving no remainder. For example, 13 is a factor of 52 because 13 divides exactly into 52 (52 ÷ 13 = 4 leaving no remainder). The complete list of factors of 52 is: 1, 2, 4, 13, 26, and 52 (all these divide exactly into 52).
function factors(n) {
var arr = [];
for(var i = 0 ; i <= n; i++){
if(n%i === 0 ) {
arr.push(i);
}
}
return arr;
}
JSON.stringify(factors(52)) === '[1, 2, 4, 13, 26, 52]';
//console.log(JSON.stringify(factors(496)));
// 4. A positive integer is perfect if it equals the sum of its factors, excluding the number itself. Choose the correct definition of the function perfects :: Int -> [Int] that returns the list of all perfect numbers up to a given limit.
function perfects(n) {
var arr = [];
// noprotect
function isPerfect(num) {
var fcts = factors(num);
var sum =0;
for(var i = 0; i < fcts.length-1; i++){
sum += fcts[i];
}
return sum === fcts[fcts.length-1];
}
for(var j = 0; j<=n; j++) {
if(isPerfect(j)){
arr.push(j);
}
}
return arr;
}
JSON.stringify(perfects(500)) === '[6, 28, 496]';
console.log(JSON.stringify(perfects(500)));
</script>
<script id="jsbin-source-javascript" type="text/javascript">// 1. Implement a map() method, which creates a new array with the results of calling a provided function on every element in this array.
Array.prototype.map = function (func) {
var arr = [];
for(var i=0; i<=this.length-1;i++){
arr[i] = func(this[i]);
}
return arr;}
JSON.stringify([1,2,3].map(function(x) { return x + 1; })) === '[2, 3, 4]';
//console.log(JSON.stringify([1,2,3].map(function(x) { return x + 1; })));
// 2. Implement filter(). Add a filter() function to the Array type. The filter() function accepts a predicate. A predicate is a function that accepts an item in the array, and returns a boolean indicating whether the item should be retained in the new array.
//...
Array.prototype.filter = function(func) {
var arr = [];
for(var i=0; i<=this.length-1;i++){
if(func(this[i])){
arr.push(this[i]);
}
}
return arr;
}
JSON.stringify([1,2,3].filter(function(x) { return x > 2})) === '[3]';
//console.log(JSON.stringify([1,2,3].filter(function(x) { return x > 2})));
// 3. Compute the factors of a positive integer. A factor is a whole number which divides exactly into a whole number, leaving no remainder. For example, 13 is a factor of 52 because 13 divides exactly into 52 (52 ÷ 13 = 4 leaving no remainder). The complete list of factors of 52 is: 1, 2, 4, 13, 26, and 52 (all these divide exactly into 52).
function factors(n) {
var arr = [];
for(var i = 0 ; i <= n; i++){
if(n%i === 0 ) {
arr.push(i);
}
}
return arr;
}
JSON.stringify(factors(52)) === '[1, 2, 4, 13, 26, 52]';
//console.log(JSON.stringify(factors(496)));
// 4. A positive integer is perfect if it equals the sum of its factors, excluding the number itself. Choose the correct definition of the function perfects :: Int -> [Int] that returns the list of all perfect numbers up to a given limit.
function perfects(n) {
var arr = [];
// noprotect
function isPerfect(num) {
var fcts = factors(num);
var sum =0;
for(var i = 0; i < fcts.length-1; i++){
sum += fcts[i];
}
return sum === fcts[fcts.length-1];
}
for(var j = 0; j<=n; j++) {
if(isPerfect(j)){
arr.push(j);
}
}
return arr;
}
JSON.stringify(perfects(500)) === '[6, 28, 496]';
console.log(JSON.stringify(perfects(500)));
</script></body>
</html>
// 1. Implement a map() method, which creates a new array with the results of calling a provided function on every element in this array.
Array.prototype.map = function (func) {
var arr = [];
for(var i=0; i<=this.length-1;i++){
arr[i] = func(this[i]);
}
return arr;}
JSON.stringify([1,2,3].map(function(x) { return x + 1; })) === '[2, 3, 4]';
//console.log(JSON.stringify([1,2,3].map(function(x) { return x + 1; })));
// 2. Implement filter(). Add a filter() function to the Array type. The filter() function accepts a predicate. A predicate is a function that accepts an item in the array, and returns a boolean indicating whether the item should be retained in the new array.
//...
Array.prototype.filter = function(func) {
var arr = [];
for(var i=0; i<=this.length-1;i++){
if(func(this[i])){
arr.push(this[i]);
}
}
return arr;
}
JSON.stringify([1,2,3].filter(function(x) { return x > 2})) === '[3]';
//console.log(JSON.stringify([1,2,3].filter(function(x) { return x > 2})));
// 3. Compute the factors of a positive integer. A factor is a whole number which divides exactly into a whole number, leaving no remainder. For example, 13 is a factor of 52 because 13 divides exactly into 52 (52 ÷ 13 = 4 leaving no remainder). The complete list of factors of 52 is: 1, 2, 4, 13, 26, and 52 (all these divide exactly into 52).
function factors(n) {
var arr = [];
for(var i = 0 ; i <= n; i++){
if(n%i === 0 ) {
arr.push(i);
}
}
return arr;
}
JSON.stringify(factors(52)) === '[1, 2, 4, 13, 26, 52]';
//console.log(JSON.stringify(factors(496)));
// 4. A positive integer is perfect if it equals the sum of its factors, excluding the number itself. Choose the correct definition of the function perfects :: Int -> [Int] that returns the list of all perfect numbers up to a given limit.
function perfects(n) {
var arr = [];
// noprotect
function isPerfect(num) {
var fcts = factors(num);
var sum =0;
for(var i = 0; i < fcts.length-1; i++){
sum += fcts[i];
}
return sum === fcts[fcts.length-1];
}
for(var j = 0; j<=n; j++) {
if(isPerfect(j)){
arr.push(j);
}
}
return arr;
}
JSON.stringify(perfects(500)) === '[6, 28, 496]';
console.log(JSON.stringify(perfects(500)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment