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

[Codewars] Remove First and Last Character (8 kyu) / JavaScript

by fluss 2022. 12. 6.

https://www.codewars.com/kata/56bc28ad5bdaeb48760009b0

 

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:

It's pretty straightforward. Your goal is to create a function that removes the first and last characters of a string. You're given one parameter, the original string. You don't have to worry with strings with less than two characters.

 

설명:

아주 간단합니다. 문자열에서 처음과 마지막 글자를 제거하는 함수를 만드세요. 원본 문자열인 매개변수 하나가 주어집니다. 두 단어 미만인 문자열에 대해 걱정할 필요가 없습니다.

 

풀이

 slice로 문자열을 1부터 마지막 전까지 잘랐다.

 

코드

function removeChar(str){
  return str.slice(1, str.length - 1);
};

 

참고

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

 

Array.prototype.slice() - JavaScript | MDN

slice() 메서드는 어떤 배열의 begin 부터 end 까지(end 미포함)에 대한 얕은 복사본을 새로운 배열 객체로 반환합니다. 원본 배열은 바뀌지 않습니다.

developer.mozilla.org

 

댓글