JSFiddle - React, Tailwind, and code Playground

Accedo job interview test prototype 2

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 OR b+c depending on config.includeA.
// - Don't get 'a' if you don't need it.
// - You can only modify the code between the START and END comments. 

angular.module('app', [])
	.constant('config', {
    	'includeA': false
    })
	.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);
            }, 100);
            return deferred.promise;
        }
        
        return {
        	get: get
        };
    })
    .controller('sumController', function sumController($scope, config, 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 = config.includeA ? data : 0;
                return dataService.get('b');
            })
            .then(function (data) {
                b = data;
                return dataService.get('c');
            })
            .then(function (data) {
                c = data;
                setResult(a + b + c);
            });

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

    });