-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChallenge-39.js
31 lines (25 loc) · 964 Bytes
/
Challenge-39.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// DESCRIPTION:
// Given two strings comprised of + and -, return a new string which shows how the two strings interact in the following way:
// When positives and positives interact, they remain positive.
// When negatives and negatives interact, they remain negative.
// But when negatives and positives interact, they become neutral, and are shown as the number 0.
// Worked Example
// ("+-+", "+--") ➞ "+-0"
// # Compare the first characters of each string, then the next in turn.
// # "+" against a "+" returns another "+".
// # "-" against a "-" returns another "-".
// # "+" against a "-" returns "0".
// # Return the string of characters.
// Examples
// ("--++--", "++--++") ➞ "000000"
// ("-+-+-+", "-+-+-+") ➞ "-+-+-+"
// ("-++-", "-+-+") ➞ "-+00"
//solutions
function neutralise(s1, s2) {
let res = "";
for (let i = 0; i < s1.length; i++) {
res += s1[i] !== s2[i] ? "0" : s1[i];
}
return res;
}
neutralise("-+-+-+", "-+-+-+");