Reputation: 35
// require '04_wonky_coins'
// require 'rspec'
//
// # Catsylvanian money is a strange thing: they have a coin for every
// # denomination (including zero!). A wonky change machine in
// # Catsylvania takes any coin of value N and returns 3 new coins,
// # valued at N/2, N/3 and N/4 (rounding down).
// #
// # Write a method `wonky_coins(n)` that returns the number of coins you
// # are left with if you take all non-zero coins and keep feeding them
// # back into the machine until you are left with only zero-value coins.
// #
// # Difficulty: 3/5
//
// describe "#wonky_coins" do
// it "handles a coin of value 1" do
// wonky_coins(1).should == 3
// end
//
// it "handles a coin of value 5" do
// wonky_coins(5).should == 11
// # 11
// # => [2, 1, 1]
// # => [[1, 0, 0], [0, 0, 0], [0, 0, 0]]
// # => [[[0, 0, 0], 0, 0], [0, 0, 0], [0, 0, 0]]
// end
//
// it "handles a coin of value 6" do
// wonky_coins(6).should == 15
// end
//
// it "handles being given the zero coin" do
// wonky_coins(0).should == 1
// end
// end
function check_coins(hand){
for(var i=0; i<hand.length; i++){
var coin = hand[i]
if(coin !==0){
return i;
} else {
return null;
}
}
return false;
}
function wonkyCoins(n){
var hand = [];
hand.push(Math.floor(n/2));
hand.push(Math.floor(n/3));
hand.push(Math.floor(n/4));
while(check_coins(hand){
var indx = check_coins(hand);
var value = hand[indx];
var index1 = hand.indexOf(hand[indx]);
if (index1 > -1) {
hand.splice(index1, 1);
}
hand.push(Math.floor(value/2));
hand.push(Math.floor(value/3));
hand.push(Math.floor(value/4));
}
return hand.length;
}
the program is working but the while loop is not looping for some reasons. I suspect that there is something wrong with the condition. I am not sure if javaScript accepts that kind of conditions.However, in ruby it worked. can someone please explain for me why is not working please?
Upvotes: 1
Views: 63
Reputation: 36
You missed a parentheses bracket to close your while loop condition parameter. Your code should really be:
while(check_coins(hand)){
...
}
If that doesn't work, it's probably the condition itself. Try:
while(check_coins(hand) !== null){
...
}
Upvotes: 2
Reputation: 8042
The while
statement should be fixed like this:
while (check_coins(hand)) {
It also appears that the check_coins
function is only checking the first coin, and should be changed to check them all. It also returns any one of an integer, boolean, or null value, depending on conditions - it should just return an integer (index of first non-zero coin) or null (no non-zero coins).
The fixed code would look like this:
function check_coins(hand){
for (var i=0; i < hand.length; i++) {
if (hand[i] > 0) {
return i;
}
}
return null;
}
function wonkyCoins(n) {
var hand = [];
hand.push(Math.floor(n/2));
hand.push(Math.floor(n/3));
hand.push(Math.floor(n/4));
while ((indx = check_coins(hand)) != null) {
var value = hand[indx];
var index1 = hand.indexOf(hand[indx]);
if (index1 > -1) {
hand.splice(index1, 1);
}
hand.push(Math.floor(value/2));
hand.push(Math.floor(value/3));
hand.push(Math.floor(value/4));
}
return hand.length;
}
The code has been cleaned up to make it more readable and idiomatic Javascript. It has not been tested, however.
Upvotes: 0