Categrory · Coding

Catching Car Mileage Numbers


“7777…8?!??!“, exclaimed Bob, “I missed it again! Argh!” Every time there’s an interesting number coming up, he notices and then promptly forgets. Who doesn’t like catching those one-off interesting mileage numbers?

Let’s make it so Bob never misses another interesting number. We’ve hacked into his car’s computer, and we have a box hooked up that reads mileage numbers. We’ve got a box glued to his dash that lights up yellow or green depending on whether it receives a 1 or a 2 (respectively).

It’s up to you, intrepid warrior, to glue the parts together. Write the function that parses the mileage number input, and returns a 2 if the number is “interesting” (see below), a 1 if an interesting number occurs within the next two miles, or a 0 if the number is not interesting.

Wrapped Function


Create a function method that allow you to wrap an existing function. The method signature would look something like this:

Usage Example:

function speak(name){
   return "Hello " + name;

speak = speak.wrap(function(original, yourName, myName){
   greeting = original(yourName);
   return greeting + ", my name is " + myName;

var greeting = speak("Mary", "Kate");

ES5 Generators(i) 和闭包 closure



This is the first part of three (part2, part3).

Generators and Iterators are new ES6 features that will allow things like this:

function* fibonacci() {
    let [prev, curr] = [0, 1];
    for (;;) {
        [prev, curr] = [curr, prev + curr];
        yield curr;

Using them in this way, we can do amazing things:

let seq = fibonacci();
print(; // 1
print(; // 2
print(; // 3
print(; // 5
print(; // 8

This is powerful, but until a few months later, ES6 will not be born.

The goal of this kata is to implement pseudo-generators with ES5.