๐ Day 6 Algorithm Review
๐ Replit
array.map()
// Convert string into number
function solution(element, index, array) {
return Number(element)
}
// Convert the last string into number
function solution(element, index, array) {
if (index === array.length-1){
element = Number(element)
}
return element
}
const arr = ['1', '2', '3'];
const result = arr.map(solution);
console.log(result); // [1, 2, 3]
String Detection Method
const email = '[email protected]';
console.log(email.startsWith('pre')); //true
console.log(email.endsWith('com')); //true
console.log(email.includes('@gmail')); //true
console.log(email.endsWith('@gmail.com')); //true
๐ Class
padStart
let str = ""
str.padStart(5, 'z') // 'zzzzz'
str.padStart(5, "*") // '*****'
slice
// 1. ๋ฐฐ์ด, ๋ฌธ์์ด์ ์๋ฅผ ๋ ์ฌ์ฉ
// 2. ์๋ณธ์ด ์ ์ฅ๋์ง ์์
str = "abcde"
str.slice(2)
๐ Ternary Operator
// ์ผํญ ์ฐ์ฐ์ ์ฌ์ฉ
function solution(num){
return num % 2 === 0? "Even" : "Odd"
}
๐ Masking phone number
function solution(phone_number){
let answer = ''
for (let i = 0; i<phone_number.length; i++){
if (i< phone_number.length -4){
answer += '*'
}
else {
answer += phone_number[i]
}
}
return answer
}
// => refactoring
function solution(phone_number){
let answer = ''
answer = answer.padStart(phone_number-4, "*")
answer += phone_number.slice(phone_number.length -4, phone_number.length)
return answer
}
๐ reduce() method
// ======== ํ๊ท ๊ฐ ๊ตฌํ๊ธฐ ========
function solution(arr){
//reduce = for loop
// ๊ณ์ ๋ฐ๋ณตํ๋๊ฑด๋ฐ reduce๋ acc(๋์ ๊ฐ)์ด๋ผ๋๊ฑฐ ์์ฒด๊ฐ ์ด์ ๊ฐ์ ๊ณ์ ๋ฐ์์ค๋๊ฑฐ์
const sum = arr.reduce( (acc, cur) => {
console.log(acc, cur) // acc = ๋์ ๊ฐ // cur = ํ์ฌ ๊ฐ
return acc + cur
},0)
return sum/arr.length
}
Author And Source
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(๐ Day 6 Algorithm Review), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค
https://velog.io/@j00b33/BE-Algorithm-Day-6-Review
์ ์ ๊ท์: ์์์ ์ ๋ณด๊ฐ ์์์ URL์ ํฌํจ๋์ด ์์ผ๋ฉฐ ์ ์๊ถ์ ์์์ ์์ ์
๋๋ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ
์ธ ๋ฐ๊ฒฌ์ ์ ๋
(Collection and Share based on the CC Protocol.)
// Convert string into number
function solution(element, index, array) {
return Number(element)
}
// Convert the last string into number
function solution(element, index, array) {
if (index === array.length-1){
element = Number(element)
}
return element
}
const arr = ['1', '2', '3'];
const result = arr.map(solution);
console.log(result); // [1, 2, 3]
const email = '[email protected]';
console.log(email.startsWith('pre')); //true
console.log(email.endsWith('com')); //true
console.log(email.includes('@gmail')); //true
console.log(email.endsWith('@gmail.com')); //true
padStart
let str = ""
str.padStart(5, 'z') // 'zzzzz'
str.padStart(5, "*") // '*****'
slice
// 1. ๋ฐฐ์ด, ๋ฌธ์์ด์ ์๋ฅผ ๋ ์ฌ์ฉ
// 2. ์๋ณธ์ด ์ ์ฅ๋์ง ์์
str = "abcde"
str.slice(2)
๐ Ternary Operator
// ์ผํญ ์ฐ์ฐ์ ์ฌ์ฉ
function solution(num){
return num % 2 === 0? "Even" : "Odd"
}
๐ Masking phone number
function solution(phone_number){
let answer = ''
for (let i = 0; i<phone_number.length; i++){
if (i< phone_number.length -4){
answer += '*'
}
else {
answer += phone_number[i]
}
}
return answer
}
// => refactoring
function solution(phone_number){
let answer = ''
answer = answer.padStart(phone_number-4, "*")
answer += phone_number.slice(phone_number.length -4, phone_number.length)
return answer
}
๐ reduce() method
// ======== ํ๊ท ๊ฐ ๊ตฌํ๊ธฐ ========
function solution(arr){
//reduce = for loop
// ๊ณ์ ๋ฐ๋ณตํ๋๊ฑด๋ฐ reduce๋ acc(๋์ ๊ฐ)์ด๋ผ๋๊ฑฐ ์์ฒด๊ฐ ์ด์ ๊ฐ์ ๊ณ์ ๋ฐ์์ค๋๊ฑฐ์
const sum = arr.reduce( (acc, cur) => {
console.log(acc, cur) // acc = ๋์ ๊ฐ // cur = ํ์ฌ ๊ฐ
return acc + cur
},0)
return sum/arr.length
}
Author And Source
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(๐ Day 6 Algorithm Review), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://velog.io/@j00b33/BE-Algorithm-Day-6-Review์ ์ ๊ท์: ์์์ ์ ๋ณด๊ฐ ์์์ URL์ ํฌํจ๋์ด ์์ผ๋ฉฐ ์ ์๊ถ์ ์์์ ์์ ์ ๋๋ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ ์ธ ๋ฐ๊ฒฌ์ ์ ๋ (Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค