# Algorithm - CodeKata #11 π

devmoonshΒ·2020λ 9μ 28μΌ
0

14/37

## 1. Question

inputμΌλ‘ λ°μ λ μλ₯Ό κ³±ν΄μ λ°νν΄μ£ΌμΈμ.
λ°ννλ ννλ λ³΅μμ ννμ string μ΄μ΄μΌ ν©λλ€.

λ³΅μμ μ μμ μνλ©΄ (i^2)λ -1 μ΄λ―λ‘ (i^2) μΌλλ -1λ‘ κ³μ°ν΄μ£ΌμΈμ.

• μ κ³± ννμ΄ μ λμ΄ iμ 2μ κ³±μ (i^2)λΌκ³  νννμ΅λλ€.

μμ  1:
Input: "1+1i", "1+1i"
Output: "0+2i"
μ€λͺ:
(1 + i) * (1 + i) = 1 + i + i + i^2 = 2i

μμ  2:
Input: "1+-1i", "1+-1i"
Output: "0+-2i"
μ€λͺ:
(1 - i) * (1 - i) = 1 - i - i + i^2 = -2i,

μμ  3:
Input: "1+3i", "1+-2i"
Output: "7+1i"
μ€λͺ:
(1 + 3i) * (1 - 2i) = 1 - 2i + 3i -6(i^2) = 1 + i + 6,

κ°μ 
inputμ ν­μ a+bi ννμλλ€.

const complexNumberMultiply = (a, b) => {
let a_container = a.split(/[-,+]/);
let b_container = b.split(/[-,+]/);
if(a_container.indexOf('') > -1){
const empty_a = a_container.indexOf('');
a_container.splice(empty_a,1)
}
if(b_container.indexOf('') > -1){
const empty_b = b_container.indexOf('');
b_container.splice(empty_b,1)
}
a_container[1] = a_container[1].slice(0,a_container[1].length-1)
b_container[1] = b_container[1].slice(0,b_container[1].length-1)
a_container = a_container.map(Number);
b_container = b_container.map(Number);
let a_operator = [];
let b_operator = [];
for(let i = 0; i < a.length; i++){
if(a[i] === '+' || a[i] === '-'){
a_operator.push(a[i])
}
}
for(let i = 0; i < b.length; i++){
if(b[i] === '+' || b[i] === '-'){
b_operator.push(b[i])
}
}

if(a_operator.length === 2){
a_container[1] = -1 * a_container[1]
}
if(b_operator.length === 2){
b_container[1] = -1 * b_container[1]
}

let container1 = []; // μ²«λ²μ¨°: μ μλΌλ¦¬, λλ²μ§Έ: μ μ*i, μΈλ²μ§Έ: iμ μ κ³± 1 + i + i + i^2 = 2i
container1[0] = a_container[0] * b_container[0]
container1[1] = a_container[0] * b_container[1] + b_container[0] * a_container[1]
container1[2] = a_container[1] * b_container[1]

let result = [];
result[0] = container1[0] - container1[2]
result[1] = container1[1]

console.log(result) // [-3, 4]
return ${result[0]}+${result[1]}i
}

console.log(complexNumberMultiply("1+2i", "1-2i")); // '-3+4i'

μΆμ²

Junior front-end developer