JSFiddle - React, Tailwind, and code Playground

Accedo job interview test prototype 1

by Csaba Hellinger

HTML

<div ng-app="app">
    <div ng-controller="sumController">
        <div ng-show="!result" class="waiting">Waiting...</div>
        <div ng-show="result">
            <div class="result">Result: {{result}}</div>
            <div class="time">{{time}} ms</div>
        </div>
    </div>
</div>

CSS

.waiting, .time {
    color: gray;
}

.result {
    font-size: 1.4em;
}

JavaScript

// Calculate a+b+c as fast as you can.
// - You can only modify the code between the START and END comments. 

angular.module('app', [])
	.service('dataService', function dataService($q, $timeout) {
    	var data = {'a': 1, 'b': 2, 'c': 3};
    
        function get(key) {
            var deferred = $q.defer(),
                value = data[key];
            $timeout(function() {
                deferred.resolve(value);
            }, value * 1000);
            return deferred.promise;
        }
        
        return {
        	get: get
        };
    })
    .controller('sumController', function sumController($scope, dataService) {
        var started = window.performance.now();

        function setResult(result) {
            $scope.result = result;
            $scope.time = Math.round(window.performance.now() - started);
        }

        // START --------------------------------------------------------

        var a, b, c;
        dataService.get('a')
            .then(function (data) {
                a = data;
                return dataService.get('b');
            })
            .then(function (data) {
                b = data;
                return dataService.get('c');
            })
            .then(function (data) {
                c = data;
                setResult(a + b + c);
            });

        // END ----------------------------------------------------------

    });