Take a two-dimensional array (an array with an array as an element) as input and return an object created using each array.
function convertListToObject(arr) {
let result = {};
for (i = 0; i < arr.length; i++) {
if (arr[i].length > 0 && result[arr[i][0]] === undefined) { // return only if arr[i] is not an empty array && if result[key]'s value is not defined.
result[arr[i][0]] = arr[i][1];
}
}
return result;
}
Reference Code:
function convertListToObject(arr) {
let result = {};
for (let i = 0; i < arr.length; i++) {
if (arr[i].length > 0 && result[arr[i][0]] === undefined) {
result[arr[i][0]] = arr[i][1];
}
}
return result;
}
Take a string as input and return a string in which all two spaces in the string are replaced with a single space.
function convertDoubleSpaceToSingle(str) {
return str.split(" ").join(" ");
}
Reference Code:
function convertDoubleSpaceToSingle(str) {
let result = '';
let before = '';
for (let i = 0; i < str.length; i++) {
// 직전의 문자가 공백이고, 현재의 문자도 공백인 경우
// 즉, 현재의 문자가 두 번째 공백인 경우(에만), 무시한다.
if (before !== ' ' || str[i] !== ' ') {
result = result + str[i];
}
before = str[i];
}
return result;
}