Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
edited tags; edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

JavaScript code challenge at an interview: A more eloquent solution? Simulated race among three robots making random steps

Source Link
gnzg
  • 133
  • 5

JavaScript code challenge at an interview: A more eloquent solution?

At a job interview, I was asked to code the following algorithm:

Given are three robots which advance across a 1km lane at a different pace respectively. Each pace made by a robot is of a random distance. All three robots stop once one of them is one pace away from reaching 1km. Once they stop, the robots' steps nearest to the middle of the lane are compared, and depending whose step throughout the journey was closest to the middle while exceeding it, a winner is declared. The winner robot's color is then printed out.

It took me a while to wrap my head around this quirky task, but I emerged with this solution:

var robot = function(color) {
    this.color = color;

    this.d1 = 0;
    this.robo1 = 0;
    this.a1 = [];
    while (this.robo1 < 1000 && this.d1 != -1) {

        // all robots begin at 0
        this.robo1 = this.robo1 + this.d1;

        // add starting point to array 
        this.a1.push(this.robo1);

        // generate new deltas
        this.d1 = Math.floor(Math.random() * 100);

        // check if adding delta to new robos would exceed 1000
        // to determine whether the next iteration 
        this.d1 = (this.d1 + this.robo1 > 1000) ? -1 : this.d1;

    }
    // log all arrays
    console.log(this.a1);

    // determine which element of a1 is closest to half of path
    console.log('smallest distance for a1:');
    this.smallestDistanceFromMiddle = 1000; // setting initial state

    this.cleanedA1 = [...this.a1];
    this.cleanedA1 = this.a1.filter(item => item > 500); // ###
    //console.log('cleanedA1 is \t' + this.cleanedA1);

    this.cleanedA1.forEach((index, item) => {
        this.iteratedValue = Math.abs(500 - this.cleanedA1[item]);
        this.smallestDistanceFromMiddle = this.iteratedValue < this.smallestDistanceFromMiddle ? this.iteratedValue : this.smallestDistanceFromMiddle;
        //console.log(iteratedValue);
    });
    console.log(this.smallestDistanceFromMiddle);
    return this.smallestDistanceFromMiddle;
}


let robot1 = new robot('red');
let robot2 = new robot('purple');
let robot3 = new robot('blue');


console.log("the robots' smallest distance to the middle is:");
console.log(robot1.color, robot1.smallestDistanceFromMiddle + '\n' +
    robot2.color, robot2.smallestDistanceFromMiddle + '\n' +
    robot3.color, robot3.smallestDistanceFromMiddle + '\n');

let nearestRobot = Math.min(robot1.smallestDistanceFromMiddle, robot2.smallestDistanceFromMiddle, robot3.smallestDistanceFromMiddle);

switch (nearestRobot) {
    case robot1.smallestDistanceFromMiddle:
        console.log(robot1.color, 'robot wins! \n');
        break;
    case robot2.smallestDistanceFromMiddle:
        console.log(robot2.color, 'robot wins! \n');
        break;
    case robot3.smallestDistanceFromMiddle:
        console.log(robot3.color, 'robot wins! \n');
        break;
    default:
        console.log('no robots defined');
}

Nevertheless, I feel like there is a more eloquent, easy on the eyes solution and I would very dearly appreciate improvement feedback.