본문 바로가기
알고리즘/Codewars

[Codewars] Grasshopper - Basic Function Fixer (8 kyu) / JavaScript

by fluss 2022. 11. 17.

https://www.codewars.com/kata/56200d610758762fb0000002

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Fix the function

I created this function to add five to any number that was passed in to it and return the new value. It doesn't throw any errors but it returns the wrong number.

Can you help me fix the function?

 

설명:

함수 수정

전달된 숫자에 5를 더하고 새로운 값을 반환하는 함수를 만들었습니다. 오류는 없지만 잘못된 숫자를 반환합니다. 

함수를 수정하는데 도움을 주실 수 있나요?

 

풀이

// 기존 함수
function addFive(num) {
  var total = num + 5
  return num
}

기존 함수는 위와 같이 5를 더한 합이 아닌 입력받은 num을 그대로 반환하고 있었다. 그래서 5를 더한 값인 total을 반환하도록 함구를 수정했다. 

 

코드

function addFive(num) {
  var total = num + 5
  return total
}

댓글