Вы находитесь на странице: 1из 3

//

//
sk.
//
//

Hack Reactor/MakerSquare/Telegraph Academy interview questions.


These are the questions to and solutions of the interview that the schools a
This is close, there may be some syntax errors
However, the ideas and logic below is what they're looking for.

// Define an array called my array with entries 1-5


var myArray = [1,2,3,4,5]
// Define an object called my object with 3 keys and 3 values
var myObject = {
blah : 'blah',
blah2: 'blah2',
blah3: 'blah3'
};
// Define a function called print while console logs the parameter you put insi
de it
var print = function(parameter){
console.log(parameter);
};
// You'll be asked varies ways of accessing an object's keys
// Here are a couple ways. That you need to know
print(myObject['blah']) or print(myObject.blah)
// They do the same thing, you're accessing the object's value using dot method
or quotation method
// You'll be asked to call the function and display the first or last element of
myArray
print(myArray[0]) //first element
print(myArray[4]) or print(myArray[myArray.length()-1]) //last element
// Define a function, each, which takes in a collection and uses a callback on
the elements of collection
// Collection can be either an array or an object
// However, JavaScript reads everything as an object.
// There are numerous ways of determining whether an object is an array, Array.
isArray() is one way
var each = function(collection, callback){
if(Array.isArray(collection)){
for(var i=0;i<collection.length;i++){
callback(collection[i]);
}
}
else{
for(var key in collection){
callback(collection[key]);
}
}
};
// Define a function, map, which uses a callback on a collection and returns an
array
// example, times3 of the array, [3,6,9,12,15]

// Create an empty array and push the


Then return the array
// You must use each function instead
// Each element of collection will be
rameter which then uses the call-back

values from the callback onto that array.


of a for-loop.
passed as an argument into the function pa
from the outer scope!

var map = function(collection, callback){


var new_Array = [];
each(collection, function(x){
new_Array.push(callback(x));
});
return new_Array;
};
//
he
//
//
//
//

Define a function filter, which checks the boolean value of the callback on t
element of collection
Here is where it gets tricky
The interviewer can ask many ways for filter to be defined.
Below covers the basic idea
You will have to modify the version they ask to fit the situation they want

// Version 1, output each boolean value of the callback


// example, even of an array, [1,2,3,4,5] -> false, true, false, true, false
var filter = function(collection, callback){
each(collection, function(item){
if(callback(item)){
return item;
}
});
};
//
//
//
//
//

Version 2, output an array of the boolean value of the callback


example, even of an array, [2,3,4,5,6] -> [true, false, true, false, true]
The interviewer may want the solution in the format below
[2,3,4,5] -> [2,4]
In any case, be ready to modify Version 2.

var filter = function(collection, callback){


map(collection, function(item){
if(callback(item)){
return item;
}
});
};
// Version 3
// example, if all elements of the array is even, output true. If one is false,
then output false
// example, [2,4,6,7] -> false
// example, [2,4,6] -> true
var filter = function(collection, callback){
var temp_solution = true;
each(collection, function(item){
if(!callback(item)){
temp_solution = false;
}
});

return temp_solution;
};
// Define a function reduce, which has an initial input value and uses the call
back on the elements of collection.
// This is the final question and the most difficult question in the interview.
I've left the format.
// Look up what reduce does and how it operates. Then figure out how to write
it from scratch.
// example, callback is sum of array, [1,2,3,4,5], initialValue is 0
-> 15
// example, callback is difference of an array, [1,2,3,4,5], initialValue is 0
-> -15
// example, callback is difference of an array, [1,2,3,4,-5], initialValue is 0
-> -5
// example, callback is multiplication of an array, [1,2,3,4,5], initialValue i
s 1 -> 120
// The point is, to write it so that the callback, whatever it is (add, mult, s
ubtract, etc.), makes changes to the initialValue and returns initialValue
var reduce = function(collection, callback, initialValue){
};

Вам также может понравиться