내배캠 32일차

·2022년 12월 15일
0

내일배움캠프

목록 보기
34/142
post-thumbnail

TODO

  • 알고리즘
  • 과제하고 제출

알고리즘

캐릭터의 좌표

function solution(keyinput, board) {
  let result = [0, 0];
  let max_x = (board[0] - 1) / 2;
  let max_y = (board[1] - 1) / 2;

  for (let i in keyinput) {
    if (keyinput[i] === "up") {
      result[1] += 1;
      if (result[1] >= max_y) {
        result[1] = max_y;
      } else if (result[1] <= -max_y) {
        result[1] = -max_y;
      }
    } else if (keyinput[i] === "down") {
      result[1] -= 1;
      if (result[1] >= max_y) {
        result[1] = max_y;
      } else if (result[1] <= -max_y) {
        result[1] = -max_y;
      }
    } else if (keyinput[i] === "left") {
      result[0] -= 1;
      if (result[0] >= max_x) {
        result[0] = max_x;
      } else if (result[0] <= -max_x) {
        result[0] = -max_x;
      }
    } else if (keyinput[i] === "right") {
      result[0] += 1;
      if (result[0] >= max_x) {
        result[0] = max_x;
      } else if (result[0] <= -max_x) {
        result[0] = -max_x;
      }
    }
  }

  return result;
}

//효율적으로
function solution(keyinput, board) {
  let result = [0, 0];
  let max_x = (board[0] - 1) / 2;
  let max_y = (board[1] - 1) / 2;

  function check_x() {
    if (result[0] >= max_x) {
      result[0] = max_x;
    } else if (result[0] <= -max_x) {
      result[0] = -max_x;
    }
  }
  
  function check_y() {
    if (result[1] >= max_y) {
      result[1] = max_y;
    } else if (result[1] <= -max_y) {
      result[1] = -max_y;
    }
  }
  
  for (let i in keyinput) {
    if (keyinput[i] === "up") {
      result[1] += 1;
      check_y();
    } else if (keyinput[i] === "down") {
      result[1] -= 1;
      check_y();
    } else if (keyinput[i] === "left") {
      result[0] -= 1;
      check_x();
    } else if (keyinput[i] === "right") {
      result[0] += 1;
      check_x();
    }
  }

  return result;
}
console.log(solution(["down", "down", "up", "up", "up"], [3, 3]));
console.log(solution(["left", "left", "left", "right"], [3, 3]));

영어가싫어요

function solution(numbers) {
    let replaced_str = 
        numbers.replace(/zero/g, 0)
               .replace(/one/g, 1)
               .replace(/two/g, 2)
               .replace(/three/g, 3)
               .replace(/four/g, 4)
               .replace(/five/g, 5)
               .replace(/six/g, 6)
               .replace(/seven/g, 7)
               .replace(/eight/g, 8)
               .replace(/nine/g, 9)
    
    return Number(replaced_str)
}
// 더 효율적
function solution(numbers) {
    const obj = {
        zero: 0, one: 1, two: 2, three: 3, four: 4,
        five: 5, six: 6, seven: 7, eight: 8, nine: 9
    };

    const num = numbers.replace(/zero|one|two|three|four|five|six|seven|eight|nine/g, (v) => {
        return obj[v];
    });

    return Number(num);
}

NODE 입문 과제

과제제출페이지
과제GITHUB

그냥 timestamp를 사용하니 실제 시간과 9시간이 차이가 나서 추가적인 작업을 하여 조회를 해주도록 하였다.

const moment = require("moment");
require("moment-timezone");
moment.tz.setDefault("Asia/Seoul");

//전체 게시글 목록 조회 API
router.get("/", async (req, res) => {
  const posts = await Posts.find(
    {},
    {
      __v: false,
      updatedAt: false,
      _id: false,
      content: false,
      password: false,
    }
  ).sort({ createdAt: -1 });

  const postList = posts.map((post) => {
    return {
      _id: post._id,
      title: post.title,
      name: post.name,
      createdAt: moment(post.createdAt).format("YYYY-MM-DD HH:mm:ss"),
    };
  });
  res.json({ postList });
});

//게시글 조회 API
router.get("/:_id", async (req, res) => {
  try {
    const { _id } = req.params;
    const post = await Posts.findOne(
      { _id },
      { __v: false, updatedAt: false, password: false }
    );

    const postDetail = {
      _id: post._id,
      title: post.title,
      name: post.name,
      content: post.content,
      createdAt: moment(post.createdAt).format("YYYY-MM-DD HH:mm:ss"),
    };

    res.json({ postDetail });
  } catch (err) {
    res.status(400).json({ result: "존재하지 않는 게시글" });
  }
});
profile
개발자 꿈나무

0개의 댓글