Skip to content

Instantly share code, notes, and snippets.

View isRuslan's full-sized avatar

Ruslan Ismagilov isRuslan

  • Yandex
  • Russia, Moscow
View GitHub Profile
@isRuslan
isRuslan / index.js
Created June 18, 2015 23:58
JS ES6: Iterable | Iterators. Play in repl: http://bit.ly/1GvjFg7
/*
Iterators | Iterables:
Iterable (data structure): Iterator (obj that enumirates data):
[Symbol.iterator]() ---> .next()
`for of` interate only on Iterables
*/
// array
@isRuslan
isRuslan / index.js
Last active August 29, 2015 14:22
JS: step_logger - print string line by line with current index letters count.
/*
Write a function step_logger(str, n) that print a string line by line from 0 to n with current index letters count.
Example:
step_logger("Hello world", 10);
H
He
Hel
@isRuslan
isRuslan / index.js
Created June 1, 2015 14:12
JS: get primitive class
function getClass(value) {
var str = Object.prototype.toString.call(value);
return /^\[object (.*)\]$/.exec(str)[1];
}
console.log(getClass(null));
console.log(getClass({}));
console.log(getClass([]));
@isRuslan
isRuslan / index.html
Last active August 29, 2015 14:22
Iframe
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>IFRAME</title>
<script src="https://gist.githubusercontent.com/isRuslan/689f8d128e2f6934f9c8/raw/a1850688a6bafa8f800a805d83f055977435b5c9/index.js"></script>
</head>
<body>
Hello from iframe!
</body>
@isRuslan
isRuslan / index.js
Created May 28, 2015 10:58
ES6 generators/iterators: Write a generator-function upper that takes an array of strings and yields each of them in upper case.
function *try_to_upper(items) {
// no effects! just iterate over `items`.
yield * items[Symbol.iterator](function * (item) {
try {
yield item.toUpperCase()
} catch (e) {
yield 'null'
}
});
}
@isRuslan
isRuslan / index.js
Created May 24, 2015 20:40
JS: numbers
var x = 3e2; // 300
var y = 3e-2; // 0.03
// only NaN dosen't equal to itself
function isNaN (number) {
return number !== number;
}
// string -> number
@isRuslan
isRuslan / index.js
Created May 22, 2015 17:47
Find the random!
var random = Math.floor(Math.random() * 100 + 1)
// your code
var guess = ...
console.assert(guess, random, 'NO!'):
@isRuslan
isRuslan / index.js
Created May 15, 2015 21:28
JS: unique array
// O(n^2): loop in loop
function unique_one (array) {
var result = [];
for (var i = 0; i < array.length; i++) {
for (var j = i; j < array.length; j++) {
if (array[i] == array[j]) {
// mmmm
}
}
@isRuslan
isRuslan / index.js
Last active August 29, 2015 14:20
You've built an in-flight entertainment system with on-demand movie streaming.
/*
Write a function that takes an integer flight_length (in minutes) and an array of integers movie_lengths (in minutes) and returns a boolean indicating whether there are two numbers in movie_lengths whose sum equals flight_length.
When building your function:
Assume your users will watch exactly two movies
Don't make your users watch the same movie twice
Optimize for runtime over memory
*/