Skip to content

Instantly share code, notes, and snippets.

@jojoblaze
Created June 29, 2018 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jojoblaze/0ffe5964a14bb4cbde24f161b6d86015 to your computer and use it in GitHub Desktop.
Save jojoblaze/0ffe5964a14bb4cbde24f161b6d86015 to your computer and use it in GitHub Desktop.
esercizio di test per candidatura Moze
/*
scrivi una funzione che a partire dall'array `input` restituisce un oggetto composto dalle seguenti proprietà:
– "near": un array di oggetti con proprietà "distance" superiore/uguale a 0 e inferiore a 4
- "medium": un array di oggetti con proprietà "distance" superiore/uguale a 4 e inferiore a 8
- "far": un array di oggetti con proprietà "distance" superiore/uguale a 8 e inferiore/uguale a 10
nell'oggetto finale non devono essere presenti oggetti con proprietà "type" uguale a "a"
*/
const input = [
{ type: 'a', distance: 5 },
{ type: 'c', distance: 3 },
{ type: 'b', distance: 6 },
{ type: 'c', distance: 12 },
{ type: 'b', distance: 1 },
{ type: 'a', distance: 5 },
{ type: 'b', distance: 6 },
{ type: 'a', distance: -1 },
{ type: 'c', distance: 9 },
{ type: 'd', distance: 10 }
];
var output = {
near: [],
medium: [],
far: []
};
var filteredA = input.filter(filterTypeA);
output.near = filteredA.filter(getNear);
output.medium = filteredA.filter(getMedium);
output.far = filteredA.filter(getFar);
function getNear(input) {
if(input.distance >= 0 && input.distance < 4)
return input;
}
function getMedium(input) {
if(input.distance >= 4 && input.distance < 8)
return input;
}
function getFar(input) {
if(input.distance >= 8 && input.distance <= 10)
return input;
}
function filterTypeA(input) {
if(input.type != 'a')
return input;
}
document.write(JSON.stringify(output));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment