Coding Test - Code Signal Intro2

All Longest Strings

1
2
3
4
5
function allLongestStrings(inputArray) {
'use strict';
let maxSize = Math.max(...inputArray.map(x => x.length));
return inputArray.filter(x => x.length === maxSize);
}

commonCharacterCount

1
2
3
4
5
6
function commonCharacterCount(s1, s2) {
for (var i = 0; i < s1.length; i++) {
s2 = s2.replace(s1[i], "!");
}
return s2.replace(/[^!]/g, "").length;
}

isLucky

1
2
3
4
5
6
function isLucky(n) {
var count = 0;
n = String(n).split('').map(t => parseInt(t));
n.forEach((el, i) => (i < n.length / 2) ? count += el : count -= el)
return count == 0
}

Sort by Height

1
2
3
4
5
6
7
8
9
10
11
12
13
function sortByHeight(a) {

var people = a.filter(el => el > 0)
people.sort((a, b) => a - b)

for (let i = 0; i < a.length; i++) {
if (a[i] !== -1) {
a[i] = people.shift()
}
}

return a
}

reverseInParentheses

1
2
3
4
5
6
function reverseInParentheses(inputString) {
while (inputString.includes('(')) {
inputString = inputString.replace(/\(([^()]*)\)/, (_, str) => [...str].reverse().join(''));
}
return inputString;
}

alternatingSums

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function alternatingSums(a) {
let teamA = []
let teamB = []
let result = []

teamA = a.filter((e, i) => i % 2 === 0)
teamB = a.filter((e, i) => i % 2 === 1)


result.push(teamA.reduce((a, b) => a + b , 0))
result.push(teamB.reduce((a, b) => a + b , 0))

return result
}