You will be given a sequence of objects representing data about developers who have signed up to attend the next coding meetup that you are organising.
Your task is to return a sequence which includes the developer who is the oldest. In case of a tie, include all same-age senior developers listed in the same order as they appeared in the original input array.
For example, given the following input array:
const list1 = [{ firstName: 'Gabriel', lastName: 'X.', country: 'Monaco', continent: 'Europe', age: 49, language: 'PHP' },{ firstName: 'Odval', lastName: 'F.', country: 'Mongolia', continent: 'Asia', age: 38, language: 'Python' },{ firstName: 'Emilija', lastName: 'S.', country: 'Lithuania', continent: 'Europe', age: 19, language: 'Python' },{ firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' }];
your function should return the following array:
[{ firstName: 'Gabriel', lastName: 'X.', country: 'Monaco', continent: 'Europe', age: 49, language:'PHP' },{ firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' }]
// Solution
function findSenior(list) {
let maxAge = 0;
list.forEach((dev) => { // ho usato forEach per trovare l'età massima tra gli sviluppatori
if (dev.age > maxAge) {
maxAge = dev.age;
}
});
return list.filter((dev) => dev.age === maxAge); // ho usato filter per filtrare gli sviluppatori con l'età massima
}
const list1 = [
{
firstName: "Gabriel",
lastName: "X.",
country: "Monaco",
continent: "Europe",
age: 49,
language: "PHP",
},
{
firstName: "Odval",
lastName: "Fabio.",
country: "Mongolia",
continent: "Asia",
age: 38,
language: "Python",
},
{
firstName: "Guea",
lastName: "Serge.",
country: "Italy",
continent: "Europe",
age: 24,
language: "Java",
},
{
firstName: "Sou",
lastName: "Bocc.",
country: "Japan",
continent: "Asia",
age: 49,
language: "PHP",
},
];
const list2 = [
{
firstName: "Gabriel",
lastName: "X.",
country: "Monaco",
continent: "Europe",
age: 49,
language: "PHP",
},
{
firstName: "Odval",
lastName: "F.",
country: "Mongolia",
continent: "Asia",
age: 38,
language: "Python",
},
{
firstName: "Emilija",
lastName: "S.",
country: "Lithuania",
continent: "Europe",
age: 19,
language: "Python",
},
{
firstName: "Sou",
lastName: "B.",
country: "Japan",
continent: "Asia",
age: 49,
language: "PHP",
},
{
firstName: "Sou",
lastName: "B.",
country: "Japan",
continent: "Asia",
age: 49,
language: "PHP",
},
];
console.log(findSenior(list1)); // [{ firstName: 'Gabriel', lastName: 'X.', country: 'Monaco', continent: 'Europe', age: 49, language:'PHP' }, { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' }]
console.log(findSenior(list2)); // [{ firstName: 'Gabriel', lastName: 'X.', country: 'Monaco', continent: 'Europe', age: 49, language:'PHP' }, { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' },
// { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' }]
function findSenior(list) {
let maxAge = 0;
list.forEach((dev) => { // ho usato forEach per trovare l'età massima tra gli sviluppatori
if (dev.age > maxAge) {
maxAge = dev.age;
}
});
return list.filter((dev) => dev.age === maxAge); // ho usato filter per filtrare gli sviluppatori con l'età massima
}
You will be given an array of objects representing data about developers who have signed up to attend the next web development meetup that you are organising. Three programming languages will be represented: Python, Ruby and JavaScript.
Your task is to return either:
true if the number of meetup participants representing any of the three programming languages is at most 2 times higher than the number of developers representing any of the remaining programming languages; or
false otherwise.
For example, given the following input array:
const list1 = [{ firstName: 'Daniel', lastName: 'J.', country: 'Aruba', continent: 'Americas', age: 42, language: 'Python' },{ firstName: 'Kseniya', lastName: 'T.', country: 'Belarus', continent: 'Europe', age: 22, language: 'Ruby' }, { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 43, language: 'Ruby' }, { firstName: 'Hanna', lastName: 'L.', country: 'Hungary', continent: 'Europe', age: 95, language: 'JavaScript' }, { firstName: 'Jayden', lastName: 'P.', country: 'Jamaica', continent: 'Americas', age: 18, language: 'JavaScript' }, { firstName: 'Joao', lastName: 'D.', country: 'Portugal', continent: 'Europe', age: 25, language: 'JavaScript' }];
your function should return false as the number of JavaScript developers (3) is 3 times higher than the number of Python developers (1). It can't be more than 2 times higher to be regarded as language-diverse.
Notes:
The strings representing all three programming languages will always be formatted in the same way (e.g. 'JavaScript' will always be formatted with upper-case 'J' and 'S'.
The input array will always be valid and formatted as in the example above.
Each of the 3 programming languages will always be represented.
function isLanguageDiverse(list) {
const counts = {};
for (let i = 0; i < list.length; i++) { // ho usato un ciclo for per contare quante volte appare un linguaggio
const language = list[i].language; // ho assegnato il linguaggio corrente a una variabile
counts[language] = counts[language] ? counts[language] + 1 : 1; // ho usato un operatore ternario per incrementare il contatore
}
const countsValues = Object.values(counts); // ho usato Object.values per ottenere i valori dell'oggetto counts
const maxCount = Math.max(...countsValues); // ho usato Math.max per trovare il valore massimo
const minCount = Math.min(...countsValues); // ho usato Math.min per trovare il valore minimo
return maxCount <= 2 * minCount; // ho usato un operatore ternario per restituire true o false
}
//Test cases
const list1 = [{ firstName: 'Daniel', lastName: 'J.', country: 'Aruba', continent: 'Americas', age: 42, language: 'Python' },
{ firstName: 'Kseniya', lastName: 'T.', country: 'Belarus', continent: 'Europe', age: 22, language: 'Ruby' },
{ firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 43, language: 'Ruby' },
{ firstName: 'Hanna', lastName: 'L.', country: 'Hungary', continent: 'Europe', age: 95, language: 'JavaScript' },
{ firstName: 'Jayden', lastName: 'P.', country: 'Jamaica', continent: 'Americas', age: 18, language: 'JavaScript' },
{ firstName: 'Joao', lastName: 'D.', country: 'Portugal', continent: 'Europe', age: 25, language: 'JavaScript' }];
const list2 = [{ firstName: 'Daniel', lastName: 'J.', country: 'Aruba', continent: 'Americas', age: 42, language: 'Python' },
{ firstName: 'Kseniya', lastName: 'T.', country: 'Belarus', continent: 'Europe', age: 22, language: 'Ruby' },
{ firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 43, language: 'Ruby' },
{ firstName: 'Hanna', lastName: 'L.', country: 'Hungary', continent: 'Europe', age: 95, language: 'JavaScript' },
{ firstName: 'Jayden', lastName: 'P.', country: 'Jamaica', continent: 'Americas', age: 18, language: 'JavaScript' },
{ firstName: 'Joao', lastName: 'D.', country: 'Portugal', continent: 'Europe', age: 25, language: 'JavaScript' },
{ firstName: 'Joao', lastName: 'D.', country: 'Portugal', continent: 'Europe', age: 25, language: 'JavaScript' }];
console.log(isLanguageDiverse(list1)); // false
console.log(isLanguageDiverse(list2)); // true
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("should test for something", function() {
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
});
});