프로그래머스 기본 알고리즘 풀이
3일차문제
1.
function solution(my_string) {
var answer = '';
answer = my_string.split('').reverse().join('')
return answer;
}
2.
function solution(my_string, letter) {
let answer = '';
a = my_string.split('').filter((item) => item !== letter)
answer = a.join('')
return answer;
}
3.
다른풀이 : repeat()이나 join()으로 간단하게 해결가능
function solution(my_string, n) {
var answer = '';
let a = my_string.split('')
let b = []
for (i=0; i<a.length; i++){
for (j=1; j<=n;j++)
b.push(a[i])+'\n'
}
answer = b.join('')
return answer;
}
4.
function solution(message) {
var answer = 0;
answer = message.length*2
return answer;
}
5.
정규식 찾아서 공부하기
function solution(my_string) {
var answer = '';
answer = my_string.replaceAll('a','')
answer = answer.replaceAll('e','')
answer = answer.replaceAll('i','')
answer = answer.replaceAll('o','')
answer = answer.replaceAll('u','')
return answer;
}
아래와같이 간단하게 정리가능
6.
function solution(str1, str2) {
var answer = 0;
if (str1.indexOf(str2) >= 0) {
answer = 1
} else {
answer =2
}
return answer;
}
4일차문제
1.
function solution(array, n) {
var answer = 0;
a = array.filter((item) => item === n)
answer = a.length
return answer;
}
2.
function solution(numbers) {
var answer = 0;
const a = numbers.reduce((c, d) => c+d)
answer = a/numbers.length
return answer;
}
3.
function solution(num_list) {
var answer = [];
a = num_list.filter((item) => item%2 === 0)
b = num_list.filter((item) => item%2 === 1)
answer = [a.length, b.length]
return answer;
}
4.
function solution(numbers, num1, num2) {
var answer = [];
for (i=num1; i<= num2 ; i++){
answer.push(numbers[i])
}
return answer
}
5.
function solution(strlist) {
let answer = [];
for (i=0; i<strlist.length; i++){
answer.push(strlist[i].length)
}
return answer;
}
6.
function solution(num_list) {
let answer = [];
answer = num_list.reverse()
return answer;
}
7.
function solution(numbers) {
let answer = 0;
numbers.sort((a,b) => b-a)
answer = numbers[0]*numbers[1]
return answer;
}
멘토타임 개별과제(6일차 개미군단)
1.
function solution(hp) {
var answer = 0;
let a = Math.trunc(hp / 5)
let hp1 = hp-a*5
let b = Math.trunc(hp1/3)
let hp2 = hp1 -b*3
let c = hp2
answer = a+b+c
return answer;
}
멘토님 순수함수 풀이
function getAntsLength(hp, ants) {
const antsArr = ants;
return antsArr.reduce((count, damage) => {
if (!hp) return count
const unitLength = Math.trunc(hp / damage);
hp %= damage;
return count += unitLength;
}, 0)
}
'항해14기 온보딩 > 항해14기 온보딩 개발일지' 카테고리의 다른 글
[항해14기] 온보딩 개발일지10 2023/03/16 (0) | 2023.03.16 |
---|---|
[항해14기] 온보딩 개발일지 9 2023/03/15 (0) | 2023.03.15 |
[항해14기] 온보딩 개발일지7 2023/03/13 (0) | 2023.03.13 |
[항해14기] 온보딩 개발일지6 2023/03/11 (0) | 2023.03.12 |
[항해14기] 온보딩 개발일지5 2023/03/10 (0) | 2023.03.10 |