Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.
It should remove all values from list a, which are present in list b keeping their order.
If a value is present in b, all of its occurrences must be removed from the other.
function arrayDiff(a, b) {
return a.filter(element => b.indexOf(element) === -1)
}
function array_diff(a, b) {
return a.filter(e => !b.includes(e));
}
function array_diff(a, b) {
b = new Set(b)
return a.filter(v => !b.has(v))
}
You probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.
Implement the function which takes an array containing the names of people that like an item. It must return the display text.
Note: For 4 or more names, the number in "and 2 others" simply increases.
function likes(names) {
let result;
if (names.length === 0) {
result = 'no one likes';
} else if (names.length === 1) {
result = `${names[0]} likes`;
} else if (names.length === 2) {
result = `${names[0]} and ${names[1]} like`;
} else if (names.length === 3) {
result = `${names[0]}, ${names[1]} and ${names[2]} like`;
} else if (names.length > 3) {
let num = names.length - 2;
result = `${names[0]}, ${names[1]} and ${num} others like`;
}
return `${result} this`;
}
function likes(names) {
names = names || [];
switch(names.length){
case 0: return 'no one likes this'; break;
case 1: return names[0] + ' likes this'; break;
case 2: return names[0] + ' and ' + names[1] + ' like this'; break;
case 3: return names[0] + ', ' + names[1] + ' and ' + names[2] + ' like this'; break;
default: return names[0] + ', ' + names[1] + ' and ' + (names.length - 2) + ' others like this';
}
}
function likes(names) {
return {
0: 'no one likes this',
1: `${names[0]} likes this`,
2: `${names[0]} and ${names[1]} like this`,
3: `${names[0]}, ${names[1]} and ${names[2]} like this`,
4: `${names[0]}, ${names[1]} and ${names.length - 2} others like this`,
}[Math.min(4, names.length)]
}
You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N. Write a method that takes the array as an argument and returns this "outlier" N.
function findOutlier(integers){
let remains = integers.map(integer => integer % 2 == 0)
let trueOrFalse = (remains.filter(e => e === true).length === 1) ? true : false;
return integers[remains.indexOf(trueOrFalse)];
}
function findOutlier(int){
var even = int.filter(a=>a%2==0);
var odd = int.filter(a=>a%2!==0);
return even.length==1? even[0] : odd[0];
}