Skip to content

Instantly share code, notes, and snippets.

@ychiang
ychiang / distru-test.js
Created May 2, 2018 20:11
Post Interview JS Question
const string = '__NAME__ came to our __EVENT__ and __ACTION__';
const opts = {
name: 'Jared',
event: 'movie',
action: 'loved it'
};
const stringReplacer = (string, opts) => {
const newString = string
@ychiang
ychiang / instance-variables.rb
Created March 27, 2018 21:21
Instance Variables Practice
class Person
attr_accessor :name
def initialize(name)
@name = name
end
end
p = Person.new('L. Ron')
puts p.name
@ychiang
ychiang / http-functions.js
Created February 20, 2018 01:00
scripting http request
var https = require('https');
var getHTML = function getHTML(options, callback) {
https.get(options, function (response) {
response.setEncoding('utf8');
var bufferedContent = "";
response.on('data', function (content) {
bufferedContent += content;
});
@ychiang
ychiang / index.js
Created February 16, 2018 20:42
Unicorn in rainbow color
var chalk = require("chalk");
const log = console.log;
log(
chalk.hex("#FA6670").bold(" FLOWERS") + " can have" + chalk.hex("#970343")(" many ") + "colors" +
" just like rainbows!\n " +
chalk.bold.red("U") + chalk.bold.keyword("orange")("N") + chalk.bold.yellow("I") + chalk.bold.green("C") + chalk.bold.blue("O") +
chalk.bold.hex("#b310de")("R") + chalk.bold.hex("#541b9f")("N") + " lives on " + "rainbows " +
chalk.bold.hex("#D4AF37")("~~~~~~~!")
@ychiang
ychiang / cheat-die.js
Created February 16, 2018 04:52
unlimited rolling of rigged dice plus countdown generator
function makeLoadedDie() {
var list = [5, 4, 6, 1, 6, 4, 2, 3, 3, 5];
var i = -1;
return function() {
i = i + 1;
if(i===list.length){
i = 0;
return list[i];
} else {
@ychiang
ychiang / map-exercise.js
Created February 16, 2018 02:22
practice more on map
var animals = ["quokka", "capybara", "hedgehog", "koala", "fluffybunny"];
//calculate length
var length = animals.map(function(animal) {
return animal.length;
});
//make them loud with uppercase
var loud = animals.map(function(animal) {
return animal.toUpperCase();
@ychiang
ychiang / sort.js
Created February 16, 2018 01:53
sort and advanced sort
var array = [1, 2, 10, 9, 5];
var sortArray = array.sort(function(a, b) {
return a - b;
});
array.sort(function(a, b) {
if (a < b) {
return -1;
} else if (a > b) {
@ychiang
ychiang / mapping-challenge.js
Created February 16, 2018 00:18
using map for callback
var input = [
{ x: 3, y: 4 },
{ x: 12, y: 5 },
{ x: 8, y: 15 }
];
var result = input.map(function(triangle) {
// console.log(Math.pow(x));
return Math.sqrt((Math.pow(triangle.x, 2) + Math.pow(triangle.y, 2)))
});
@ychiang
ychiang / callback.js
Created February 15, 2018 22:14
using callback function with for loop and foreach
var findWaldo = function(arr, found) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === "Waldo") {
found(i);
}
}
}
function actionWhenFound(index) {
console.log("Found Waldo at index " + index);
@ychiang
ychiang / character-positioning.js
Created February 14, 2018 21:11
Find the position of characters
function countLetters(sentences) {
var obj = {};
for (var i = 0; i < sentences.length; i++) {
if (sentences[i] !== ' ') {
if (obj[sentences[i]] === undefined) {
obj[sentences[i]] = [];
}
obj[sentences[i]].push(i);
}
}