destination([
['B', 'C'],
['D', 'B'],
['C', 'A'],
]); // 'A'
destination([
['Kwangju', 'Busan'],
['Seoul', 'Ansan'],
['Ansan', 'Kwangju'],
]); // 'Busan'
function destination(paths) {
// 여기에서 작업하세요.
let answer = [];
paths.sort();
for (let i = 0; i < paths.length; i++) {
for (let j = 0; j < paths.length; j++) {
if (paths[i][1] === paths[j][0] && paths[j][1] !== paths[i][0]) {
answer.push(paths[j][1]);
}
}
}
return answer[0];
}
function destination(paths) {
// 여기에서 작업하세요.
let nextDestination = paths[0][1];
for (let i = 0; i < paths.length; i++) {
for (let j = 0; j < paths.length; j++) {
if (paths[j][0] === nextDestination) {
nextDestination = paths[j][1];
}
}
}
return nextDestination;
}
function destination(paths) {
// 여기에서 작업하세요.
const map = new Map(paths);
let station = paths[0][0];
while (map.has(station)) {
station = map.get(station);
}
return station;
}
function destination(paths) {
// 여기에서 작업하세요.
const starts = paths.map((el) => el[0]);
const destinations = paths.map((el) => el[1]);
const answer = destinations.filter((el) => !starts.includes(el));
return answer[0];
}
sortString('codi2ng to3gether let1s') // 'lets coding together'
function sortString(str) {
// 여기에서 작업하세요.
let answer = "";
str = str.split(" ");
for (let i = 0; i < str.length; i++) {
for (let j = 0; j < str.length; j++) {
if (str[i].includes(j)) {
answer += str[i] + " ";
}
}
}
return answer;
}
function sortString(str) {
// 여기에서 작업하세요.
const answer = [];
str = str.split(' ');
for (let i = 0; i < str.length; i++) {
let num = '';
let newWord = '';
for (let j = 0; j < str[i].length; j++) {
const isNumber = !isNaN(str[i][j]);
isNumber ? (num += str[i][j]) : (newWord += str[i][j]);
}
answer[num - 1] = newWord;
}
return answer.join(' ');
}
function sortString(str) {
// 여기에서 작업하세요.
const answer = [];
str = str.split(' ');
for (let word of str) {
let num = '';
for (let el of word) {
num += !isNaN(el) ? el : '';
}
word = word.replace(num, '');
answer[num - 1] = word;
}
return answer.join(' ');
}
stringInObject({
a: 'hello',
b: ['abc'],
c: 'world',
d: 100,
e: false
}) // 2
const obj = {
a: 1,
b: 2,
c: 3
}
stringInObject(obj)
function stringsInObject(obj) {
// 여기에서 작업하세요.
let answer = 0;
obj = Object.values(obj);
for (let i = 0; i < obj.length; i++) {
if (typeof obj[i] === "string") {
answer++;
}
}
return answer;
}
function stringsInObject(obj) {
// 여기에서 작업하세요.
let count = 0;
for (let key in obj) {
if (typeof obj[key] === 'string') {
count++;
}
}
return count;
}
function stringsInObject(obj) {
// 여기에서 작업하세요.
const objKeys = Object.keys(obj).filter((key) => {
return typeof obj[key] === 'string';
});
return objKeys.length;
}
function stringsInObject(obj) {
// 여기에서 작업하세요.
return Object.keys(obj).reduce((acc, cur) => {
return acc + (typeof obj[cur] === 'string' ? 1 : 0);
}, 0);
}
triangle(3, 4, 5) // 'right'
function triangle(a, b, c) {
// 여기에서 작업하세요.
let answer = "";
if (a ** 2 + b ** 2 === c ** 2) {
answer = "right";
} else {
answer = "wrong";
}
return answer;
}
function triangle(a, b, c) {
// 여기에서 작업하세요.
return a * a + b * b === c * c ? 'right' : 'wrong';
}
출처: 코드캠프