2
\$\begingroup\$

I recently applied for a job as a node/react developer. I'm attempting to cross over from a research position to web development. I didn't get the job but all the feedback I got was that my code was too academic and that it would take me too long to get up to their requirements. I'm not really sure what this means so any insight in to the code much appreciated. Obviously I'd like to improve. Also, general tips on how to improve code in all aspects (blogs, books etc) very welcome.

Here are the two challenges on my git hub:

https://github.com/thelxinoe/MRMFrontEndInterview

This one was posed as a front end challenge and the idea is to make all 9 of the tests pass.

Question 1

Modify the contents of 'app/test1.js' and implement the functions: filterNames, objectFilter, compoundObjectFilter filterNames - should take an array of strings and a search string. It should return an array of names that startwith the search string objectFilter - should take an array of objects, a key, and a search string It should return an array of objects that have a key that is equal to the search string compoundObjectFilter - should take an array of objects, a string with multiple keys seperated by "."s and a search string. It shold return an array of objects that have a key that equals the search string.

Answer 1.

export function filterNames(names, startsWith){
    //Use the built in array and string methods to filter the array.
    return names.filter(function(name){
        return name.startsWith(startsWith)
    })
}


export function objectFilter(objects, key, searchString) {
    return objects.filter(function(object){
        return (key in object) && object[key]==searchString
    })
}


export function compoundObjectFilter(objects, keysString, searchString) {
    //break the keysString in to its constituent keys to look for
    let keyStrings = keysString.split('.');
    //use filter to find all of those that have the searchstring 
    //in the value at the end of the application of the keys
    return objects.filter(function(object){
        var currentVal = object;
        //Go through all the keys applying them to the object
        for(var i = 0; i < keyStrings.length; i++){
            //if the key exists place the result in the current value
            if(keyStrings[i] in currentVal){
                currentVal = currentVal[keyStrings[i]];
            }else{
                //otherwise return false
                return false
            }
        }
        //finally check for the searchstring at the end of the key chain
        return currentVal == searchString
    })
}

export function filterNames(names, startsWith){
    //Use the built in array and string methods to filter the array.
    return names.filter(function(name){
        return name.startsWith(startsWith)
    })
}

export function objectFilter(objects, key, searchString) {
    return objects.filter(function(object){
        return (key in object) && object[key]==searchString
    })
}

export function compoundObjectFilter(objects, keysString, searchString) {
    //break the keysString in to its constituent keys to look for
    let keyStrings = keysString.split('.');
    //use filter to find all of those that have the searchstring 
    //in the value at the end of the application of the keys
    return objects.filter(function(object){
        var currentVal = object;
        //Go through all the keys applying them to the object
        for(var i = 0; i < keyStrings.length; i++){
            //if the key exists place the result in the current value
            if(keyStrings[i] in currentVal){
                currentVal = currentVal[keyStrings[i]];
            }else{
                //otherwise return false
                return false
            }
        }
        //finally check for the searchstring at the end of the key chain
        return currentVal == searchString
    })
}

Question 2.

Modify the contents of 'app/test2.js' and implement the functions: capitalise, extractValue, extractCompountValue capitalise - should take an array of strings It should return a new array with the strings capitalised extractValue - should take an array of objects and a key It should return an array of values for the key extractCompountValue - should take an array of objects, a string with multiple keys seperated by "."s It shold return an array of values for the compound key

export function capitalise(names){
    return names.map(function(name){
       return name.toUpperCase()
    })
}

export function extractValue(objects, key) {
    return objects.map(function(object){
        return object[key]
   })
}

export function extractCompoundValue(objects, keysString){
    //split the compound key to an array
    let keyStrings = keysString.split('.');
    return objects.map(function(object){
        var currentVal = object;
        //Go through all the keys applying them to the object
        for(var i = 0; i < keyStrings.length; i++){
            //if the key exists place the result in the current value
            if(keyStrings[i] in currentVal){
                currentVal = currentVal[keyStrings[i]];
            }else{
                //otherwise return undefined
                return undefined
            }
        }
        //finally return value
        return currentVal
    })
}

Question 3.

Modify the contents of 'app/test3.js' and implement a function that will return an array of the fibbonnacci sequence.

The function must take an number argument and generate that many numbers for the sequence.

Answer 3.

export default function test3(numMax){
    let result = [0,1]
    for(var i=0; i<numMax-2;i++){
        result.push(result[i]+result[i+1]);
    }
    return result.splice(0,numMax)
}

Question 4.

Modify the contents of 'app/test4.js' and implement a function that will return an array of prime numbers.

The function must take an number argument and generate all the prime numbers that appear BEFORE the argument.

Answer 4.

export default function getPrimes(max){
    if(max==1 || max==2){
        return []
    }


    let primes = [2]
    if(max==3){
        return primes
    }
    for(var i=3; i<=max; i=i+2){
        primes.push(i)
    }
    var j=1
    while(primes[j]!=undefined){
        primes = primes.slice(0,j+1).concat(primes.slice(j+1).filter(function(prime){
            return !(prime % primes[j]==0)
        }))
        j++
    }
    return primes.splice(0,max)

 }

Question 5.

Modify the contents of 'app/test5.js' and implement the functions: groupBySex groupBySex - should take an array of people It should return a new object with keys for the value of sex and values containing array of people with that sex groupByYearThenSex - should take an array of poeple It shoudl return a new object with keys that are the year the person was born which should have values with keys for the sex which should have values that are arrays of people born that year with that sex. e.g.

{ '1971': { male: [ [Object], [Object] ], female: [ [Object] ] },
  '1972': { male: [ [Object], [Object] ], female: [ [Object] ] } }

Answer 5.

export function groupBy(people, key) {
    var grouped = {};
    people.forEach(function(peep){
        if(grouped[peep[key]]==undefined){
            grouped[peep[key]]=[peep]
        }
        else{
            grouped[peep[key]].push(peep)
        }
    })
    return grouped

}

export function groupBySex(people) {
    return groupBy(people,'sex')
}

export function groupByYearThenSex(people) {

    var groupedByYear=groupBy(people,'born')
    for(var year in groupedByYear){
        groupedByYear[year]=groupBy(groupedByYear[year],'sex')
    }

    return groupedByYear

}

Question 6.

Modify the contents of 'app/test6.js' and add 3 new classes - English, French and Spanish that extend the Greeting class. Override the "sayHello" method to say hello in the correct language

Answer 6.

class Greeting {
  constructor(name) {
    this.name = name;
  }
  sayHello() {
    throw new Error('I don\'t know how to speak!')
  }
}

// Add your new classes here
export class English extends Greeting {
    constructor(name) {
        super(name);
    }
    sayHello(){
        return 'Hello '+this.name
    }
}

export class French extends Greeting {
    constructor(name) {
        super(name);
    }
    sayHello(){
        return 'Bonjour '+this.name
    }

}

export class Spanish extends Greeting {
    constructor(name) {
        super(name);
    }
    sayHello(){
        return 'Hola '+this.name
    }

}

Question 7.

Modify the contents of 'app/test7.js' and write a function that will extend String so you can call "String".reverse() The extension must return the string passed in, but in reverse order.

Answer 7.

export default function test7(string){
  //Write your solution here
    String.prototype.reverse = function(){
        return Array.from(this).reverse().join('')
    }
  //
  return string.reverse();
}

Question 8.

Modify the contents of 'app/test8.js' and write a function that will add all the numbers from 0 to 100. You must not use the + operator. This includes ++ and +=

Answer 8.

export function add(x,y){
    while(y!=0){
        var carry = x & y;
        x = x ^ y;
        y = carry<<1;
    }
    return x
}

export default function test8(){
    var sum = 1
    for (var i=2;i<=100;i++){
        sum = add(sum,i);
    }
    return sum
}

Question 9.

Modify the contents of 'app/test9.js' and implement a function that counts the nodes in a binary tree

Answer 9.

export default function countNodes(root) {
    var count = 1;
    if (root.left!=null){
        count += countNodes(root.left)
    }
    if (root.right!=null){
        count += countNodes(root.right)
    }
    return count;
}

I moved the restful api to another post.

Thanks,

\$\endgroup\$
  • \$\begingroup\$ What was the approximate amount of time spent between this question and its partner? \$\endgroup\$ – 1252748 Oct 11 '16 at 12:46
  • \$\begingroup\$ I spent maybe a couple of hours on this and maybe 4 or so hours on the other. I was mostly banging my head against a wall trying to get mongo tests to run. \$\endgroup\$ – user1170304 Oct 11 '16 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.