Skip to content

Commit

Permalink
2024-05-07 다항식 더하기
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghdrn1399 committed May 7, 2024
1 parent 10a3af0 commit ba00b4e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions honggukang0623/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
|:----:|:---------:|:----:|:-----:|:----:|
| 1차시 | 2024.03.26 | 사칙연산 |[분수의덧셈]https://school.programmers.co.kr/learn/courses/30/lessons/120808 | [#5]https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/5 |
| 2차시 | 2024.03.29 | 배열 |[배열두배만들기]https://school.programmers.co.kr/learn/courses/30/lessons/120809 | [#7]https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/7 |
<<<<<<< Updated upstream
=======
| 3차시 | 2024.04.05 | 배열 |[중앙값구하기]https://school.programmers.co.kr/learn/courses/30/lessons/120811# | [#13]https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/13 |
| 4차시 | 2024.04.09 | 출력 |[직각삼각형출력하기]https://school.programmers.co.kr/learn/courses/30/lessons/120823 | [#17]https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/17/commits|
>>>>>>> Stashed changes
36 changes: 36 additions & 0 deletions honggukang0623/다항식 더하기/다항식 더하기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function solution(polynomial) {
const values = polynomial.split(' + ');
console.log("values : ", values);
let x = 0;
let c = 0;
for(let i=0;i<values.length;i++) {
const item = values[i];
if(item[item.length-1] !== 'x') {
c += Number(item);
}
else {
// x일때
const num = item.split('x')[0];
if(num === '') {
x += 1;
} else {
x += Number(num)
}
}
}
let answer = '';
if(x === 1) {
answer += 'x';
}
if(x > 1) {
answer += `${x}x`;
}

if(x === 0 && c > 0) {
answer += c;
}
else if(c > 0) {
answer += ` + ${c}`;
}
return answer;
}

0 comments on commit ba00b4e

Please sign in to comment.