3.5 KiB
3.5 KiB
title, id, challengeType
| title | id | challengeType |
|---|---|---|
| GeneratorExponential | 5a23c84252665b21eecc7e7b | 5 |
Description
Instructions
Tests
tests:
- text: '''<code>exponentialGenerator</code> should be a function.'''
testString: 'assert(typeof exponentialGenerator==''function'',''<code>exponentialGenerator</code> should be a function.'');'
- text: '''<code>exponentialGenerator()</code> should return a number.'''
testString: 'assert(typeof exponentialGenerator(10)==''number'',''<code>exponentialGenerator()</code> should return a number.'');'
- text: '''<code>exponentialGenerator(10)</code> should return <code>144</code>.'''
testString: 'assert.equal(exponentialGenerator(10),144,''<code>exponentialGenerator(10)</code> should return <code>144</code>.'');'
- text: '''<code>exponentialGenerator(12)</code> should return <code>196</code>.'''
testString: 'assert.equal(exponentialGenerator(12),196,''<code>exponentialGenerator(12)</code> should return <code>196</code>.'');'
- text: '''<code>exponentialGenerator(14)</code> should return <code>256</code>.'''
testString: 'assert.equal(exponentialGenerator(14),256,''<code>exponentialGenerator(14)</code> should return <code>256</code>.'');'
- text: '''<code>exponentialGenerator(20)</code> should return <code>484</code>.'''
testString: 'assert.equal(exponentialGenerator(20),484,''<code>exponentialGenerator(20)</code> should return <code>484</code>.'');'
- text: '''<code>exponentialGenerator(25)</code> should return <code>784</code>.'''
testString: 'assert.equal(exponentialGenerator(25),784,''<code>exponentialGenerator(25)</code> should return <code>784</code>.'');'
Challenge Seed
function exponentialGenerator (n) {
// Good luck!
}
Solution
function exponentialGenerator(n){
function* PowersGenerator(m) {
var n=0;
while(1) {
yield Math.pow(n, m);
n += 1;
}
}
function* FilteredGenerator(g, f){
var value = g.next().value;
var filter = f.next().value;
while(1) {
if( value < filter ) {
yield value;
value = g.next().value;
} else if ( value > filter ) {
filter = f.next().value;
} else {
value = g.next().value;
filter = f.next().value;
}
}
}
var squares = PowersGenerator(2);
var cubes = PowersGenerator(3);
var filtered = FilteredGenerator(squares, cubes);
var curr=0;
for(var i=0;i<n;i++) curr=filtered.next();
return curr.value;
}