728x90
반응형
[자바스크립트] String을 Number 타입으로 바꾸기 포스팅 썸네일 이미지

개발/Javascript

[자바스크립트] String을 Number 타입으로 바꾸기

자바스크립트는 명시적인 데이터타입 정의가 없습니다. Int나 String같이 타입을 명시해서 변수를 정의하지 않고 그냥 var 타입으로 정의하면 자바스크립트가 알아서 적절한 타입을 지정합니다. 명시적인 타입이 없다는 것은 때론 타입 때문에 헷갈리기도 하고 원치 않은 결과가 나오기도 합니다. 그래서 보통은 약간 편법적인 방법으로 String을 number로 바꾼다던지 Number를 String으로 바꾼다던지 합니다. 위의 방법이 가장 간단하게 형변환을 하는 방법입니다. 쉽게 말해서 자바스크립트의 자동형변환을 이용해서 처리한 것입니다. 숫자타입에 문자열을 더하면 결과가 문자열이 되고 문자열에 숫자를 곱하면 숫자타입이 되는 특성을 이용해서 결과는 달라지지 않게 타입만 변환되도록 처리한 것입니다. 예제를 해본 ..

2016.09.18 게시됨

[자바스크립트] 배열 요소 중 최대값 최소값 찾기 포스팅 썸네일 이미지

개발/Javascript

[자바스크립트] 배열 요소 중 최대값 최소값 찾기

자바스크립트에는 숫자로 이루어진 배열 내에서 가장 큰 숫자 혹은 가장 작은 숫자를 찾아야 할 경우 for문을 돌리는 단순한 방법 외에 더 간단한 방법이 존재합니다! 먼저 Math 객체를 사용하는 방법이 있습니다. Math 객체에 가장 큰 숫자, 가장 작은 숫자를 구하는 max, min 메서드가 존재하는데 여기에 apply 메서드를 사용하면 숫자로 이루어진 배열을 파라미터로 전달할 수 있게 됩니다. 아래의 예시를 보면 이해하기 쉬울겁니다. 한 가지 조심해야할 부분이 있는데, 배열 내에 비교 불가능한 값이 있다면 결과는 NaN으로 나오게 됨을 주의해야 합니다.

2016.09.18 게시됨

[자바스크립트로 구현한 알고리즘] Highest and Lowest 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Highest and Lowest

In this little assignment you are given a string of space separated numbers, and have to return the highest and lowest number.Example:highAndLow("1 2 3 4 5"); // return "5 1"highAndLow("1 2 -3 4 5"); // return "5 -3"highAndLow("1 9 3 4 -5"); // return "9 -5" Notes:· All numbers are valid Int32, no need to validate them.· There will always be at least one number in the input string. · Output stri..

2016.09.18 게시됨

[자바스크립트로 구현한 알고리즘] Complementary DNA 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Complementary DNA

Description: Deoxyribonucleic acid (DNA) is a chemical found in the nucleus of cells and carries the "instructions" for the development and functioning of living organisms. If you want to know more http://en.wikipedia.org/wiki/DNA In DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G". You have function with one side of the DNA (string, except for Haskell); you need to..

2016.09.17 게시됨

[자바스크립트로 구현한 알고리즘] Disemvowel Trolls 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Disemvowel Trolls

Trolls are attacking your comment section! A common way to deal with this situation is to remove all of the vowels from the trolls' comments, neutralizing the threat. Your task is to write a function that takes a string and return a new string with all vowels removed. For example, the string "This website is for losers LOL!" would become "Ths wbst s fr lsrs LL!". 모음만 제거하는 예제다. 정규표현식 /[aeiou]/ gi..

2016.09.17 게시됨

[자바스크립트로 구현한 알고리즘] Beginner Series #3 Sum of Numbers 포스팅 썸네일 이미지

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Beginner Series #3 Sum of Numbers

Given two integers, which can be positive and negative, find the sum of all the numbers between including them too and return it. If both numbers are equal return a or b. Note! a and b are not ordered! Example: GetSum(1, 0) == 1 // 1 + 0 = 1 GetSum(1, 2) == 3 // 1 + 2 = 3 GetSum(0, 1) == 1 // 0 + 1 = 1 GetSum(1, 1) == 1 // 1 Since both are same GetSum(-1, 0) == -1 // -1 + 0 = -1 GetSum(-1, 2) ==..

2016.09.17 게시됨

Computer science/알고리즘

[자바스크립트로 구현한 알고리즘] Count by X

Create a function with two arguments that will return a list of length (n) with multiples of (x). Assume both the given number and the number of times to count will be positive numbers greater than 0.Return the results as an array (or list in Python, Haskell or Elixir). Examples: countBy(1,10) === [1,2,3,4,5,6,7,8,9,10] countBy(2,5) === [2,4,6,8,10] 이런 방법으로 첫 코딩을 작성했다. 좀 더 보기 좋은 코드로는 이렇게 작성도 가능하다.

2016.09.12 게시됨

728x90
반응형