William Hill Test

by Andy Jones

HTML

<h1>
 William Hill Tech test
</h1>
<p>
A series of numbers starts with 1, 1, 1.<br />

After that, each term is the sum of the terms two and three places behind.<br />

So, the first few terms of the series look like this: <br />

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15<br />
1 1 1 2 2 3 4 5 7 9 12 16 21 28 37 49<br />
and so on.<br />
In other words:<br />

f( 0 ) = 1<br />
f( 1 ) = 1<br />
f( 2 ) = 1<br />
f( n ) = f(n  - 2 ) + f( n - 3)<br />
The exercise:<br />
Write a function f which accepts a parameter n.<br />
The function should return the value at position n in the series.<br />
</p>
Answer: <pre id="output" class="output"></pre>

CSS

.output {
  font-size: 20px;
  font-weight: bold;
}

Babel + JSX

out = (...args) =>{
    document.getElementById('output').innerHTML += args.join(" ") + "\n";
}


const f = n => Array.from({length: (n+1)}, (value, key) => {
	if (key <= 2) {
    return 1
  } else {
    return f(key-2) + f(key-3)
  }
}).reduce((i, n) => n);

out(f(15));