This file contains hidden or 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 triangle = "#"; triangle.length <= 7; triangle += "#") | |
console.log(triangle); |
This file contains hidden or 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 = 1; i <= 100; i++) { | |
if (i%3==0 && i%5==0) | |
console.log("FizzBuzz"); | |
else if (i%3==0) | |
console.log("Fizz"); | |
else if (i%5==0) | |
console.log("Buzz"); | |
else | |
console.log(i); | |
} |
This file contains hidden or 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 gridSize = Number(prompt("Enter size of grid", "8")); | |
var totalSize = (gridSize * gridSize) + gridSize; | |
var grid = ""; | |
for (i = 0; i < totalSize; i++) { | |
if (i % (gridSize + 1) == 0) | |
grid += "\n"; | |
else if (i % 2 == 0) | |
grid += "#"; | |
else | |
grid += " "; |
This file contains hidden or 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 min(a,b) { | |
var result = b; | |
if (a < b) | |
result = a; | |
return result; | |
} | |
console.log(min(0, 10)); | |
// → 0 | |
console.log(min(0, -10)); |
This file contains hidden or 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 isEven(num) { | |
if (num == 0) | |
return true; | |
if (num == 1) | |
return false; | |
if (num < 0) | |
return "??"; | |
else return isEven(num - 2); | |
} |
This file contains hidden or 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 countBs(s) { | |
var count = 0; | |
for (var i = 0; i < s.length; i += 1) { | |
if (s.charAt(i) === "B") | |
count += 1; | |
} | |
return count; | |
} | |
function countChar(s, c) { |
This file contains hidden or 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
// step parameter is optional | |
// if step is not passed in, | |
// and start is less than or equal to end, | |
// then step = 1, else step = -1 | |
function range(start, end, step = start <= end ? 1 : -1) { | |
let result = []; | |
// loop iterates up for positive step values | |
// and iterates down for negative step values | |
for (let i = start; step >= 0 ? i <= end : i >= end; i+=step) { | |
result.push(i); |
This file contains hidden or 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 reverseArray(array) { | |
result = []; | |
for (let item of array) { | |
result.unshift(item); | |
} | |
return result; | |
} | |
function reverseArrayInPlace(array) { | |
let len = array.length; |
This file contains hidden or 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 arrayToList(array) { | |
let result = {}; | |
if (Array.isArray(array)) { | |
let currListItem = result; | |
for (let item of array) { | |
let newListItem = { | |
value: item, | |
rest: null | |
}; | |
if (typeof currListItem.rest === 'undefined') { |
This file contains hidden or 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 deepEqual(a, b) { | |
if (a === b) { | |
// items are identical | |
return true; | |
} else if (typeof a === 'object' && a !== null && typeof b === 'object' && b !== null) { | |
// items are objects - do a deep property value compare | |
// join keys from both objects together in one array | |
let keys = Object.keys(a).concat(Object.keys(b)); | |
// filter out duplicate keys | |
keys = keys.filter( |
OlderNewer