alert(getJosephus(getOrdinalsUpTo(getCount(1, 100))));
function getJosephus(queue) {
let lastRemoved;
while (queue.length > 0) {
queue.push(queue.shift());
lastRemoved = queue.shift();
}
return lastRemoved;
}
function getOrdinalsUpTo(n) {
const ordinals = [];
for (let ordinal = 1; ordinal <= n; ordinal++) {
ordinals.push(ordinal);
}
return ordinals;
}
function getCount(lower, upper) {
const p = "Enter a positive integer greater than " +
lower - 1 + " and less than " + upper + ".";
let value;
while (!Number.isInteger(value) || value < lower || value >= upper) {
response = prompt(p);
if (response === null) return 0;
value = parseFloat(response);
}
return value;
}
Exercises
- The program shown above is defective. Fix it.
- Replace
getCount with getRandomCount.
- In
getOrdinalsUpTo, replace [] with new Queue.
- Implement
Queue using linked nodes.
- Implement
Queue using a fixed-length array. (Do not use methods of Array.)
- Implement
getJosephus by manipulating the bits of the base two representation of the input value.
(See Finding Josephus.)
- Implement and use
getRebels instead of getOrdinalsUpTo.
- Have
getRebels return a list of names. (Use getRandomName?)
- Have
getRebels return a list of Rebel objects. (Use getRandomRebel?)
- Replace
getJosephus with getJosephusSequence.
- Repeat these exercises using other programming languages.