문법> 

 

switch (조건 값)

{

    case 값1:

        조건 값이 값1일 때 실행하고자 하는 명령문;

        break;

    case 값2:

        조건 값이 값2일 때 실행하고자 하는 명령문;

        break;

    ...

    default:

        조건 값이 어떠한 case 절에도 해당하지 않을 때 실행하고자 하는 명령문;

        break;

}

중복되지 않은 항목들의 그룹이며,

좀 더 엄밀히 얘기하자면 중복을 허용하지 않고 정렬되지 않은 항목들을 그룹이다. 따라서 정의에 맞게 그 용도 역시 항목의 유일성을 확인하는 것입니다.

 

ex)

 

const set = new Set([1, 3, 5]);

// looping through Set
set.forEach(myFunction);

function myFunction(item) {
    console.log(item);
}
 
output>
1
3
5
 

function returnNum() {
    return 3;
}
 
console.log(1 + console.log(3)); 
console.log(1 + returnNum());

 

 

 

 

 

output

>  Nan

>  4

 

- ex)

3
happy
new
year 

가 있으면 마지막 year 뒤에 앤터키가 들어있으면 마지막은 원래 문자열 그대로 이며 

3,happy new 같은경우는 전부 앤터가 들어간경우여서 length 를출력하면 +1 해서 나온다.

 

- stack.push(array[i].split(" ")[1]); 에서 [1]은 

=> 입력값이

push 1

이거일때 push 다음 1을 뜻한다. 

- 정규식을 이용하여 모든 문자열 치환

문자열 안에 변경하려는 문자열을 여러개 있고 모든 문자열을 바꾸고 싶을 때, 정규식을 이용하여 모든 문자열을 변경할 수 있습니다. replace(/[old str]/g, '[new str]')는 문자열에 있는 모든 old string을 new string으로 변환합니다. 아래 예제에서 /Java/g가 정규 표현식인데, 정규표현식은 /Pattern/flag처럼 패턴과 플래그로 구성됩니다. 그리고 주의할 점은 찾으려는 문자열에 따옴표를 입력하지 않아야 합니다.

아래 예제는 문자열에 있는 Java를 모두 JavaScript로 변환하는 예제입니다. 플래그 g는 모든 문자열을 변환하라는 의미입니다.

let str = 'Hello world, Java, Java, Java';

str = str.replace(/Java/g, 'JavaScript');
console.log(str);

Output:

Hello world, JavaScript, JavaScript, JavaScript

https://www.acmicpc.net/problem/2941

 

2941번: 크로아티아 알파벳

예전에는 운영체제에서 크로아티아 알파벳을 입력할 수가 없었다. 따라서, 다음과 같이 크로아티아 알파벳을 변경해서 입력했다. 크로아티아 알파벳 변경 č c= ć c- dž dz= đ d- lj lj nj nj š s= ž z=

www.acmicpc.net

const fs = require("fs");
const input = (
  process.platform === "linux"
    ? fs.readFileSync("/dev/stdin").toString() // 여기선 dev로 해도됨 
    : `ljes=njak`
).trim();

let croatia = ["c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="];

function solution(input) {
  for (let alphabet of croatia) {
    input = input.split(alphabet).join("Q");
     //console.log(input) 확인용 
<ljes=njak
ljes=njak
ljes=njak
ljes=njak
Qes=njak
Qes=Qak
QeQQak
QeQQak
6> 이런식으로 순차접근 

 
 
  }

  return input.length; // return input일 경우 QeQQak를 반환한다.
}

console.log(solution(input)); //6 

 

for...of

for...of 명령문 반복가능한 객체 (Array, Map, Set, String, TypedArray, arguments 객체 등을 포함)에 대해서 반복하고 각 개별 속성값에 대해 실행되는 문이 있는 사용자 정의 반복 후크를 호출하는 루프를 생성합니다.

ex)

const array1 = ['a', 'b', 'c'];

for (const element of array1) {
  console.log(element);
}

// expected output: "a"
// expected output: "b"
// expected output: "c"

 

> "a"

> "b"

> "c"

 

구문

    for (variable of iterable) {
      statement
    }
variable
각 반복에 서로 다른 속성값이 variable에 할당됩니다.

iterable

반복되는 열거가능(enumerable)한 속성이 있는 객체.

 

Array.prototype.concat()

concat() 메서드는 인자로 주어진 배열이나 값들을 기존 배열에 합쳐서 새 배열을 반환합니다.

  • 기존배열을 변경하지 않습니다.
  • 추가된 새로운 배열을 반환합니다.

ex)

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);

console.log(array3);
// expected output: Array ["a", "b", "c", "d", "e", "f"]

 

output>

 Array ["a", "b", "c", "d", "e", "f"]

+ Recent posts