Dec 24, 2016

Second level of CodeFights: Arcade, At the Crossroads. If you haven't solved them and are planning to, don't peek, it won't do you any good to cheat. Want to codefight?

Reach Next Level (300/300)

You are playing an RPG game. Currently your experience points (XP) total is equal to experience. To reach the next level your XP should be at least at threshold. If you kill the monster in front of you, you will gain more experience points in the amount of the reward.

Given values experiencethreshold and reward, check if you reach the next level after killing the monster.


You know this one was an absolute gift.

function reachNextLevel(experience, threshold, reward) {
    return experience+reward >= threshold;

Knapsack Light (300/300)

You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the second item weighs weight2 and is worth value2. What is the total maximum value of the items you can take with you, assuming that your max weight capacity is maxW and you can't come back for the items later?


function knapsackLight(value1, weight1, value2, weight2, maxW) {
    var max_items = 0;
    //Whatever has more value is the first thing to come with us
    if(value1 < value2){
        tmp_value = value1;
        value1 = value2;
        value2 = tmp_value;
        tmp_weight= weight1;
        weight1 = weight2;
        weight2 = tmp_weight;
    var after_first_item = maxW - weight1;
    max_items = after_first_item >= 0 ? value1 : 0;
    after_first_item = after_first_item >= 0 ? after_first_item : maxW;
    var after_second_item = after_first_item - weight2;
    max_items += after_second_item >= 0 ? value2 : 0;
    return max_items;

Extra Number (300/300)


function extraNumber(a, b, c) {
    var diff = a;
        diff = c;
    else if ( a == c)
        diff = b;
    return diff;

Is Infinite Process? (300/300)

You're given three integers, ab and c. It is guaranteed that two of these integers are equal to each other. What is the value of the third integer?


function isInfiniteProcess(a, b) {
    return a > b || 
        ( Math.abs(a - b) % 2 ) != 0;

Arithmetic Expression (300/300)

Consider an arithmetic expression of the form A#B=C. Check whether it is possible to replace # with one of the four signs: +-* or / to obtain a correct expression.


var add = function (a,b,c){
    return (a + b) == c ;    

var subs = function (a,b,c){
    return (a - b) == c ;    

var mult= function (a,b,c){
    return (a * b) == c ;    

var divide = function (a,b,c){
    if (b==0) return false;
    return (a / b) == c ;    

function arithmeticExpression(A, B, C) {
    var math_operations = [add, subs, mult, divide];
    var possible = false;
    for(var i=0; i<math_operations.length;++i){
        if(  math_operations[i](A,B,C) ){ // For robustness, should check--> typeof fn === 'function'
    return possible;

Tennis Set (300/300)

In tennis, a set is finished when one of the players wins 6 games and the other one wins less than 5, or, if both players win at least 5 games, until one of the players win 7 games.

Determine if it is possible for a tennis set to be finished with the score score1 : score2.


function tennisSet(score1, score2) {
    var possible = false;
    //One won 6 games and other less than 5
    if( score1==6 || score2==6 )
        possible = (score1+score2-6) < 5;
    //Both won at least 5, and then one got to 7
    if(!possible && score1!=score2 && (score1>=5 && score2>=5) )    
        possible = score1==7 || score2==7;
    return possible;

Will you? (300/300)

Once Mary heard a famous song, and a line from it stuck in her head. That line was "Will you still love me when I'm no longer young and beautiful?". Mary believes that a person is loved if and only if he/she is both young and beautiful, but this is quite a depressing thought, so she wants to put her belief to the test.

Knowing whether a person is youngbeautiful and loved, find out if they contradict Mary's belief.

A person contradicts Mary's belief if one of the following statements is true:

  • they are young and beautiful but not loved;
  • they are loved but not young or not beautiful.


function willYou(young, beautiful, loved) {
    var contradicted = young && beautiful && !loved;
    // First test passed, keep digging for contradiction
        contradicted = (loved && !young) || (loved && !beautiful);
    return contradicted;

Metro Card (300/300)

You just bought a public transit card that allows you to ride the Metro for a certain number of days.

Here is how it works: upon first receiving the card, the system allocates you a 31-day pass, which equals the number of days in January. The second time you pay for the card, your pass is extended by 28 days, i.e. the number of days in February (note that leap years are not considered), and so on. The 13th time you extend the pass, you get 31 days again.

You just ran out of days on the card, and unfortunately you've forgotten how many times your pass has been extended so far. However, you do remember the number of days you were able to ride the Metro during this most recent month. Figure out the number of days by which your pass will now be extended, and return all the options as an array sorted in increasing order.


function metroCard(lastNumberOfDays) {
    //Months w/ 28 days: Feb
    //Months w/ 30 days: Apr, June, Sept, Nov
    //Months w/ 31 days, Jan, Mar, May, Jul, Aug, Oct, Dec
    var options = [31]; //Assuming lastNumberOfDays == 28 or 30
    if( lastNumberOfDays==31 )
        options = [28, 30, 31];
    return options;