https://school.programmers.co.kr/learn/courses/30/lessons/118666?language=javascript
const solution = (survey, choices) => {
const characterTypesArray = ["R", "T", "C", "F", "J", "M", "A", "N"];
const category = new Map();
characterTypesArray.forEach((elem) => category.set(elem, 0));
// [ 1, -1, -2, 3, 1 ]
const score = choices.map((value) => value - 4);
// items "AN" -> ["A", "N"] 인자 한개 item
survey.forEach((surveyOptions, surveyIndex) => {
const scoreCritia = surveyOptions[1]; // 상대적인 점수이므로
category.set(scoreCritia, category.get(scoreCritia) + score[surveyIndex]);
// return (category[scoreCritia] += score[surveyIndex]);
});
const checkedTypesScore = [...category];
console.log("checked", checkedTypesScore);
let answerArray = [];
for (let i = 0; i < checkedTypesScore.length; i += 2) {
let firstElement = checkedTypesScore[i];
let secondElement = checkedTypesScore[i + 1];
if (firstElement[1] > secondElement[1]) {
answerArray.push(firstElement[0]);
} else if (firstElement[1] < secondElement[1]) {
answerArray.push(secondElement[0]);
} else if (firstElement[1] === secondElement[1]) {
let sorted = [firstElement[0], secondElement[0]].sort((a, b) => a - b);
answerArray.push(sorted[0]);
}
}
return answerArray.join("");
};

일단 Map에다가 점수들 세팅해놓고
survey에다가 4씩 빼줌 (기준이 4니까)
그리고 점수가 상대값이니까 오른쪽 점수에만 점수 더해줌
점수 구한 것들 같이 Map을 배열로 바꾸고
for문 돌린 뒤 점수 비교해서 answerArray에 넣고 조인시킴
뻘짓 목록.. (완전 초보적인 실수)
1. lenth라고 써놓고 왜 포문 안돌아가지 함
2. 포문 안에 리턴 찍어놓고 왜 포문 끝나지..함
3. 변수명 개똥쓰레기임
4. 빈 배열 선언하고 넣는 방식말고 떠오르는 것이 없다ㅎ
정말 바보다.
시간 복잡도는 내가 보기에 O(n)으로 무난한 듯


나와 비슷한 접근법이지만 더 나은 풀이

다른 사람 풀이 (동적 할당, 삼항연산자 사용)
function solution(survey, choices) {
var answer = '';
let indi = new Map();
['R','T', 'C','F', 'J','M', 'A', 'N'].forEach(item => indi.set(item, 0));
choices.forEach((item,index) => {
let [A, B] = survey[index].split('');
if(item > 4) indi.set(B, indi.get(B)+item-4);
else if(item < 4) indi.set(A, indi.get(A)+4-item);
})
answer += indi.get('R') >= indi.get('T') ? 'R' : 'T';
answer += indi.get('C') >= indi.get('F') ? 'C' : 'F';
answer += indi.get('J') >= indi.get('M') ? 'J' : 'M';
answer += indi.get('A') >= indi.get('N') ? 'A' : 'N';
return answer;
}
와 대박 개쩐다 동적할당을 저렇게 쓰는구나
const { response } = require("express");
const http = require("http");
const cookie = require("cookie");
http
.createServer(function (req, res) {
res.writeHead(200, {
"Set-Cookie": ["yummy_cookie = choco", "tasty_cookie = strawberry"],
});
console.log(req.headers.cookie);
// string: yummy_cookie=choco; tasty_cookie=strawberry
const cookies = cookie.parse(req.headers.cookie); // object { yummy_cookie: 'choco', tasty_cookie: 'strawberry' }
console.log(cookies);
res.end("Coooookie");
})
.listen(3000);

parse() 메서드는 undefined 못 읽으므로 에러처리 해줘야함
세션쿠키: 브라우저가 켜져있는 동안에만 유효한 쿠키
영구쿠키: 브라우저를 꺼도 계속 저장되어 있는 쿠키
-> (
Max-age: 지금부터 얼마동안 유효한지 지정 / 단위~ sec,
Expires: 쿠키를 언제 해지할지 지정
)
const { response } = require("express");
const http = require("http");
const cookie = require("cookie");
http
.createServer(function (req, res) {
res.writeHead(200, {
"Set-Cookie": [
"yummy_cookie = choco",
"tasty_cookie = strawberry",
`permanent_cookie = cookies; Max-age = ${60 * 60 * 24 * 30}`,
],
});
const originalCookie = req.headers.cookie;
if (originalCookie) {
const cookies = cookie.parse(originalCookie); // object { yummy_cookie: 'choco', tasty_cookie: 'strawberry' }
}
res.end("Coooookie");
})
.listen(3000);
쿠키 이름 = 값 ; [쿠키 옵션]
Secure는 https에서만
Httponly는 http에서만 (콘솔에서 js로 못불러옴 document.cookie 명령어 써도 이 옵션 달린 쿠키는 못 봄)
추석이라 쉬엄쉬엄함!