Cover image for Meta
Logo
Afficher toutes les photos

Meta

Employeur impliqué

Meta

Ajouter un entretien

Questions d'entretien

Entretien pour Front End Engineer

-

Meta

1. In JavaScript, write a function that takes an array as input that can contain both ints and more arrays (which can also contain an array or int) and return the flattened array. ex. [1, [2, [ [3, 4], 5], 6]] => [1, 2, 3, 4, 5, 6] 2. Using HTML and CSS, show how you would create an image that would display another image (aligned to the bottom, right) when the user hovers over the image. ex. The Facebook "edit profile picture" icon

Réponses aux questions d'entretien

12 réponse(s)

4

ary.join().split(',');

leo le

3

array.toString().split(',').map(Number), map need because input array has only integer types.

alex le

5

var flatten = function(arr, resultArr) { var result = resultArr || []; for(var i = 0; i < arr.length; i++) { if(Array.isArray(arr[i])) { flatten(arr[i], result); } else { result.push(arr[i]); } } return result; };

Vinnie le

1

Alex's solution: [1, [2, [ [3, 4], 5], 6]].toString().split(",").map(Number) Works but I think it's missing the point. I would think the question is trying to see how you can traverse nested structures. My solution traverses the array and does it recursively, which I think is appropriate when traversing nested structures: function flatten(arr){ var newArr = []; arr.forEach(function iterate(el){ if( el instanceof Array){ el.forEach(iterate); }else{ newArr.push(el); } }); return newArr; } flatten([1, [2, [ [3, 4], 5], 6]]); CSS question is fairly easy. Given two img elements with the classes "one" and "two", you can give them the styles: .two{ display:none; } .one:hover + .two{ display: block; }

spliter le

0

Alex's solution: [1, [2, [ [3, 4], 5], 6]].toString().split(",").map(Number) Works but I think it's missing the point. I would think the question is trying to see how you can traverse nested structures. function flatten(arr){ var newArr = []; arr.forEach(function iterate(el){ if( el instanceof Array){ el.forEach(iterate); }else{ newArr.push(el); } }); return newArr; } flatten(a);

Utilisateur anonyme le

0

function flatten(arr) { var flatArray = []; function _flatten(value) { if (typeof value === 'number') { flatArray.push(value); } else { value.map(function(value) { _flatten(value); }); } } arr.map(_flatten); return flatArray; }

Nabil Boag le

0

arr.join().split(',').map(function(i) { return parseInt(i) });

Utilisateur anonyme le

0

const flatten = arr => arr.reduce((a, b) => b instanceof Array ? a.concat(flatten(b)) : a.concat(b), [])

Utilisateur anonyme le

0

function flatten (array) { var flattened = []; function helper (arr) { for (var i = 0; i < arr.length; i++) { if (typeof arr[i] === 'object') { helper(arr[i]); } else { flattened.push(arr[i]); } } } helper(array); return flattened; }

Tay Yang Shun le

1

/* * Similar to Vinnie's answer, but more performant with fewer accessors */ function flattenArray(arr, dest) { var flatArray = dest || [], n = arr.length, i, val; for (i = 0; i < n; i++) { val = arr[i]; if (Array.isArray(val)) { flattenArray(val, flatArray); } else { flatArray.push(val); } } return flatArray; }

Jason M le

1

[].concat.apply([], arr);

James le

1

1.Flatten an array using JavaScript : var flatten = function(input, output) { if(!output) output = []; var i= 0, l= input.length; for (; i < l; i++){ var value = input[i], isArray = toString.call(value) === "[object Array]"; if(isArray) { flatten(value, output); }else { output.push(value); } }; return output; } var arr = [4, [3, 6, [9, 1, 9, [5, 1]]], 8, [5]]; console.log(flatten(arr)); 2. Use CSS Sprites.

Antoine D le

Ajouter des réponses ou des commentaires

Pour commenter ceci, connectez-vous ou inscrivez-vous.

Carrières chez Meta

Cover image for Meta

Build for the future with Meta. Meta is building for the future by developing innovative ways to build community and bring people closer...Plus

  • What We Build
  • Our Actions
  • Our Community
C'est l'occasion pour les employeurs de vous dire pourquoi vous devriez travailler pour eux. Les informations fournies correspondent à leur point de vue.