Created
October 21, 2016 21:07
-
-
Save TaylorAckley/9cc4566443c111c48ce767737fcde349 to your computer and use it in GitHub Desktop.
JS Excersises
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
//1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... | |
function fibonacci(n) { | |
var a = 1; | |
var b = 0; | |
var temp; | |
while (n >= 0) { | |
temp = a; | |
a = a + b; | |
b = temp; | |
n--; | |
} | |
return b | |
} | |
//console.log(fibonacci(12)); | |
Array.prototype.aIndexOf = function(element) { | |
for (var i = 0; i < this.length; i++) | |
if (this[i] === element) { | |
return i; | |
} | |
return -1; | |
} | |
var arr = [1, 2, 4, 5, 9, 10, 12]; | |
//console.log(arr.aIndexOf(4)); | |
function createArrBy3s(n) { | |
var ret = []; | |
var sv = 0; | |
while(n >=0) { | |
if(sv % 3 === 0) { | |
ret.push(sv); | |
console.log(ret); | |
n--; | |
} | |
sv++ | |
} | |
return ret; | |
} | |
function highestCommonDivisor(n1, n2) { | |
} | |
var sample2 = [1, 2, [3, 4], 7, [9, 10], 12]; | |
function flattenArray(arr) { | |
return arr.reduce(function(a, b) { | |
console.log(a); | |
return a.concat(b); | |
}, []); | |
} | |
var flat = flattenArray(sample2) | |
console.log(flat); | |
function isPrime(n) { | |
if(n === 1) { | |
return true; | |
} | |
var divisor = 2; | |
while (n > divisor) { | |
if (n % divisor === 0) { | |
return false; | |
} | |
divisor++; | |
} | |
return true; | |
} | |
console.log(isPrime(137)); | |
var sample = [1,2,3,4,5]; | |
function duplicate(arr) { | |
return sample.concat(sample); | |
} | |
console.log(duplicate(sample)); | |
//console.log(createArrBy3s(24)); | |
var sample3 = ['taylor', 'George']; | |
var dude = sample3.map(function(x) { | |
return x + '.dude'; | |
}); | |
console.log(dude); |
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
for(var i = 0; i < 100; i++) { | |
if(i % 3 === 0 && i % 5 === 0) { | |
console.log(i + ' fizzbuzz'); | |
} else if (i % 5 === 0) { | |
console.log(i + ' buzz'); | |
} else if (i % 3 === 0) { | |
console.log(i + ' fizz'); | |
} | |
} |
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
function Person(name, email, locale) { | |
this.name = name; | |
this.email = email; | |
this.locale = locale | |
this.greet = function() { | |
console.log('Hello ' + name); | |
} | |
} | |
Person.prototype.assignWeapon = function() { | |
var weapons = ['nunchuck', 'sword', 'axe', 'brass knuckles', 'chain', 'spear']; | |
function getRandomInt() { | |
var min = Math.floor(0); | |
var max = Math.ceil(weapons.length); | |
return Math.floor(Math.random() * (max - min) + min); | |
} | |
var token = getRandomInt(); | |
console.log(token); | |
this.weapon = weapons[token]; | |
}; | |
Person.prototype.countTo100byPrime(); | |
var sally = new Person('Sally Mae', 'testing@test.com', 'Seattle'); | |
sally.greet(); | |
sally.assignWeapon(); | |
console.log(sally.weapon); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment