Skip to content

Instantly share code, notes, and snippets.

View macikokoro's full-sized avatar

Joaquin Guardado macikokoro

View GitHub Profile
@macikokoro
macikokoro / forSheep.js
Created June 28, 2014 23:36
For loop that prints sheeps.
var numSheep = 4;
var monthsToPrint = 12;
for(var monthNumber = 1; monthNumber <= monthsToPrint; monthNumber++){
numSheep*=4;
console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
}
@macikokoro
macikokoro / generator.js
Created June 29, 2014 00:53
For and while loop combo to print 19 generators total MW output.
var currentGen = 1;
var totalGen = 19;
var totalMW = 0;
while(currentGen<=4){
totalMW += 62;
console.log("Generator #" + currentGen + " is on, adding 62 MW, for a total of " + totalMW + " MW!");
currentGen++;
}
for(currentGen = 5; currentGen <= totalGen; currentGen++){
@macikokoro
macikokoro / forConditional.js
Last active August 29, 2015 14:03
For loop with conditionals.
var totalCars = 10;
var carsOperational = 8;
var carNumber = 1;
for (var carNumber = 1; carNumber <= totalCars; carNumber++) {
if (carNumber <= carsOperational){
console.log("Car # " + carNumber + " is running.");
}else{
console.log("Car # " + carNumber + " is not running.");
}
@macikokoro
macikokoro / even.js
Created June 29, 2014 01:55
While loop that checks for even numbers in descending order.
var num = 10;
while(num > 0) {
if (num % 2 === 0) {
console.log(num);
}
num--;
}
@macikokoro
macikokoro / sheepRemove.js
Last active August 29, 2015 14:03
Loop that checks for set amount, divides it by two and then removes half.
var numSheep = 4;
var monthsToPrint = 12;
for(var monthNumber = 1; monthNumber <= monthsToPrint; monthNumber++) {
if (numSheep > 10000) {
numSheep = numSheep / 2;
console.log("Removing " + numSheep + " sheep from the population. Phew!");
}
numSheep*=4;
console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
}
@macikokoro
macikokoro / prime.js
Last active August 29, 2015 14:03
Function to check for prime numbers.
function prime(n) {
var isPrime = true;
for (var i = 2; i < n && isPrime; i++) {
if (n % i === 0) {
isPrime = false;
}
}
return isPrime;
}
@macikokoro
macikokoro / oneTwenty.js
Created June 29, 2014 07:38
For loop that counts from eight until 120 by increments of 12.
for (var i = 8 ; i < 120; i+= 12) {
console.log(i);
}
@macikokoro
macikokoro / tenToZero.js
Created June 29, 2014 07:46
For loop example that will countdown to zero.
for (var i = 10; i >= 0; i--) {
console.log(i);
}
@macikokoro
macikokoro / junk.js
Created June 29, 2014 07:55
Simple example of an array.
var junk = ["uno", "dos", 3, 4 ];
console.log(junk);
@macikokoro
macikokoro / arrayLoop.js
Created June 29, 2014 07:59
Simple example of an array with a loop.
var cities = ["Melbourne", "Amman", "Helsinki", "NYC"];
for (var i = 0; i < cities.length; i++) {
console.log("I would like to visit " + cities[i]);
}