Dec 23, 2016

This belongs to a series of posts about CodeFights: Arcade. If you don't know CodeFights then I strongly suggest you sign up to the site (it's free) and start battling and playing arcade mode to improve your coding skills. It's fun and it works your brain! Want to codefight?

If you haven't solved them then you shouldn't read on so that you can solve them on your own, that's how you improve.

Let's get started with Intro Gates. 

Add Two Digits (300/300)

You are given a two-digit integer n. Return the sum of its digits.


I just treated the n as a String, looped it then added it's characters (the digits) after parsing them to ints with '+';

function addTwoDigits(n) {
    var n_asstring = n + "";
    return  +n_asstring[0] + +n_asstring[1]; 


In this case I went for the "pop" each digit option and sum them up.

int addTwoDigits(int n) {
    int result = 0;
    int digit;
    while(n>0){ //Loop through number, pop each digits and add it
        digit =  n % 10; //Pop digit
        result += digit;  
        n = n/10; //Remove popped digit
    return result;

Largest Number (300/300)

Given an integer n, return the largest number that contains exactly n digits.


function largestNumber(n) {
    var starter = "";
        starter += "9";
    return +starter;


int largestNumber(int n) {
    // To get the largest number of n Digits, build number made of of 1s and multiply by 9
    // n=2 --> 11*9  == 99
    int set_of_ones = 1;
        set_of_ones *= 10;
    return set_of_ones*9;

Candies (300/300)

n children have got m pieces of candy. They want to eat as much candy as they can, but each child must eat exactly the same amount of candy as any other child. Determine how many pieces of candy will be eaten by all the children together. Individual pieces of candy cannot be split.


function candies(n, m) {
    return Math.floor(m/n) * n;

Seats in Theater (300/300)

Your friend advised you to see a new performance in the most popular theater in the city. He knows a lot about art and his advice is usually good, but not this time: the performance turned out to be awfully dull. It's so bad you want to sneak out, which is quite simple, especially since the exit is located right behind your row to the left. All you need to do is climb over your seat and make your way to the exit.

The main problem is your shyness: you're afraid that you'll end up blocking the view (even if only for a couple of seconds) of all the people who sit behind you and in your column or the columns to your left. To gain some courage, you decide to calculate the number of such people and see if you can possibly make it to the exit without disturbing too many people.

Given the total number of rows and columns in the theater (nRows and nCols, respectively), and the row and column you're sitting in, return the number of people who sit strictly behind you and in your column or to the left, assuming all seats are occupied.


function seatsInTheater(nCols, nRows, col, row) {
    var new_cols = nCols - col + 1; //+1 b/c you want to include your column
    var new_rows = nRows - row; // not a +1 b/c you DON'T want to include your row
    return new_cols * new_rows;

Max Multiple (300/300)

Given a divisor and a bound, find the largest integer N such that:

  • N is divisible by divisor.
  • N is less than or equal to bound.
  • N is greater than 0.

It is guaranteed that such a number exists.


function maxMultiple(divisor, bound) {
    var result = divisor;
    for(var i = 2; divisor*i <= bound; ++i){
        result = divisor * i;        
    return result;

Circle of Numbers (300/300)

Consider integer numbers from 0 to n - 1 written down along the circle in such a way that the distance between any two neighbouring numbers is equal (note that 0 and n - 1 are neighbouring, too).

Given n and firstNumber, find the number which is written in the radially opposite position to firstNumber.


For this problem I simply treated the radially opposite position as the ones that is 180º to given position.

function circleOfNumbers(n, firstNumber) {
    var separated_by_degrees = 360 / n; //Circle is made up of 360 degrees
    var firstNumberDegrees = firstNumber*separated_by_degrees;
    var radially_opposite = firstNumberDegrees >= 180 ? -180 : 180;
    return (firstNumberDegrees + radially_opposite) / separated_by_degrees;

Late Ride (300/300)

One night you go for a ride on your motorcycle. At 00:00 you start your engine, and the built-in timer automatically begins counting the length of your ride, in minutes. Off you go to explore the neighborhood.

When you finally decide to head back, you realize there's a chance the bridges on your route home are up, leaving you stranded! Unfortunately, you don't have your watch on you and don't know what time it is. All you know thanks to the bike's timer is that n minutes have passed since 00:00.

Using the bike's timer, calculate the current time. Return an answer as the sum of digits that the digital timer in the format hh:mm would show.


function lateRide(n) {
    var hours = Math.floor(n/60);
    hours = hours < 10 ? "0" + hours : hours + "";
    var minutes = (n % 60) + "";
    minutes = minutes < 10 ? "0" + minutes : minutes + "";
    return +hours[0] + +hours[1] + +minutes[0]  + +minutes[1];

Phone Call (300/300)

Some phone usage rate may be described as follows:

  • first minute of a call costs min1 cents,
  • each minute from the 2nd up to 10th (inclusive) costs min2_10 cents
  • each minute after 10th costs min11 cents.

You have s cents on your account before the call. What is the duration of the longest call (in minutes rounded down to the nearest integer) you can have?


function phoneCall(min1, min2_10, min11, s) {
    var first_10_mins_price = min1 + min2_10*9;
    var first_minutes = s / first_10_mins_price; 
    var remaining_minutes = 0;
    if(first_minutes >= 1){
        first_minutes = Math.floor(first_minutes)*10; // B/c the first minutes price is for 10mins        
        var remaining_cash = s % first_10_mins_price; 
        remaining_minutes = Math.floor(remaining_cash/min11) // B/c every minute from now on is min11cents   
        first_minutes = Math.floor(first_minutes*10);       
    return first_minutes + remaining_minutes;