* fix: removed assert msg argument * fix: removed msgs surrounded by 2 single quotes * fix: removed missing 2 assert msg arguments * fix: remove msg surrounded by two single quotes * fix: removed unnecessary assert msg args * fix; remove msgs surrounded by double quotes * fix: removed unnecessary assert msg args * fix: remove unnecessary assert msg args * fix: removed unnecessary assert msg arg * fix: removed unnecessary assert msg args * fix: removed unnecessary assert msg arg * fix: removed unnecessary assert msg args * fix: removed unnecessary assert msg args * fix: removed unnecessary assert msg args * fix: removed unnecessary assert msg args * fix: removed unnecessary assert msg args * fix: removed unnecessary assert msg arg * fix: removed unnecessary assert msg args * fix: Restore expected values to assertions * fix: remove assertion message Co-authored-by: Vivek Agrawal <vivekmittalagrawal@gmail.com>
3.1 KiB
3.1 KiB
title, id, challengeType
title | id | challengeType |
---|---|---|
Josephus problem | 5a23c84252665b21eecc7ec5 | 5 |
Description
Instructions
Tests
tests:
- text: <code>josephus</code> should be a function.
testString: assert(typeof josephus=='function');
- text: <code>josephus(30,3)</code> should return a number.
testString: assert(typeof josephus(30,3)=='number');
- text: <code>josephus(30,3)</code> should return <code>29</code>.
testString: assert.equal(josephus(30,3),29);
- text: <code>josephus(30,5)</code> should return <code>3</code>.
testString: assert.equal(josephus(30,5),3);
- text: <code>josephus(20,2)</code> should return <code>9</code>.
testString: assert.equal(josephus(20,2),9);
- text: <code>josephus(17,6)</code> should return <code>2</code>.
testString: assert.equal(josephus(17,6),2);
- text: <code>josephus(29,4)</code> should return <code>2</code>.
testString: assert.equal(josephus(29,4),2);
Challenge Seed
function josephus(init, kill) {
// Good luck!
}
Solution
function josephus(init, kill) {
var Josephus = {
init: function(n) {
this.head = {};
var current = this.head;
for (var i = 0; i < n - 1; i++) {
current.label = i + 1;
current.next = {
prev: current
};
current = current.next;
}
current.label = n;
current.next = this.head;
this.head.prev = current;
return this;
},
kill: function(spacing) {
var current = this.head;
while (current.next !== current) {
for (var i = 0; i < spacing - 1; i++) {
current = current.next;
}
current.prev.next = current.next;
current.next.prev = current.prev;
current = current.next;
}
return current.label;
}
}
return Josephus.init(init).kill(kill)
}