function convertListToObject(arr) {
//입력: 2차원 배열
//출력: arr[i]의 첫번째 요소를 키, 두번째 요소를 값으로 한다.
let result = {};
for(let el of arr){
if(!el.length || result[el[0]]) continue; // 빈배열일때, 키가 이미 있을때 패스
result[el[0]] = el[1];
}
return result;
}
wow [[]].length === 1임!!
그러나 [[]] == false이다. 오오 이걸 활용해서도 풀 수 있을듯.
function convertDoubleSpaceToSingle(str) {
//입력: 문자열
//출력: 두칸의 공백을 한칸의 공백으로 바꾸어 리턴.
//str.split(' ') 한 후 arr.join('')한다.
return str.split(' ').join(' ');
}
function numberSearch(str) {
//1. 숫자가 있다면 모두 더한다.
//2. 문자열의 길이로 나눈다.
//3. 정수로 반올림해 리턴한다.
if(!str.length) return 0;
let num = 0;
let leng = 0;
for(let char of str) {
if(char === ' '){
leng;
} else if(Number(char)) {
num = num + Number(char);
} else {
leng++;
}
}
return Math.round(num / leng);
}
반올림 Math.round()
parseInt는 소수점 버림
function decryptCaesarCipher(str, secret) {
//입력: 암호화된 문자열, 키(카이사르 암호)
//출력: 복호화된 문자열
let startAt = 97;
let endAt = 122;
let result = '';
//char === 97~122라면?
//result = result + (char - secret)
for(let char of str){
if(startAt <= char.charCodeAt() && endAt >= char.charCodeAt()){
//만일 char - secret이 97~122를 벗어나게 된다면?
let translateChar = char.charCodeAt() - secret;
if(translateChar < 97) { //97 이하일때 +26
translateChar = translateChar+26;
} else if (translateChar > 122){ //122 이상일때 -26
translateChar = translateChar - 26;
}
result = result + String.fromCharCode(translateChar);
} else {
result = result + char;
}
}
return result;
}