Create a Basic JavaScript Object

 

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/object-oriented-programming/create-a-basic-javascript-object

 

www.freecodecamp.org

 

사람들이 매일 보는 것들을 생각해보자, 자동차나, 상점, 새들 같은 것들..

이런 것들이 객체이다

사람들이 관찰하고, 상호작용할 수 있는 명백한 것들.

 

이런 객체들에게는 어떤 특징(퀄리티)들이 있을까?

자동차는 바퀴가 있고, 상점은 물건을 팔고, 새는 날개가 있다

 

이러한 특징들 또는 특성들은 객체를 구성하는 것들을 정의한다.

비슷한 객체들은 같은 특성을 공유한다. 그러나 그러한  특성들에서 차이가 있을 수도 있다.

예를 들어 모든 자동차는 바퀴가 있지만, 모든 자동차의 바퀴 개수가 같은 것은 아닌 것 처럼!

 

자바스크립트에서 객체는 실제 세상에서의 객체를 모델로 하고있다. 각 개체들에게 특성을 부여하고, 행동을 부여한다.

여기 오리(duck)의 예시가 있다 

let duck = {
  name: "Aflac",
  numLegs: 2
};

duck은 두개의 특성과 값이 있다. 이름은 Aflac 이고, 다리 숫자는 2이다

 

그럼, dog 객체를 name 과 numLegs 특성을 갖도록 만들어보자. 각각은 string, number 타입을 갖는다

 

let dog = {
  name : "Mike",
  numLegs : 4
};

Chunky Monkey

 

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/chunky-monkey

 

www.freecodecamp.org

 

첫번째 arr을 주어진 size(숫자)의 길이를 갖는 배열로 잘라내는 함수를 작성하시오

그리고 그 짜갈른 결과를 어레이에 담아서 2차원 배열로 리턴하라

 

function chunkArrayInGroups(arr, size) {
  let trial = arr.length/size
  let anw = []
  for(let t=0; t<trial; t++){
    anw.push(arr.slice(size*t, size*(t+1)))
  }
  return anw;
}

chunkArrayInGroups(["a", "b", "c", "d"], 2);

 

 

function chunkArrayInGroups(arr, size) {
  const newArr = [];
  for (let i = 0; i < arr.length; i += size) {
    newArr.push(arr.slice(i, i + size));
  }
  return newArr;
}

chunkArrayInGroups(["a", "b", "c", "d"], 2);

 

Mutations

 

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations

 

www.freecodecamp.org

 

만약 주어진 arr의 첫번째 string이 두번째 string의 letter(철자)들을 모두 가지고 있다면 true를 리턴하라

예를 들어 ['hello', 'Hello'] 가 주어진다면, true를 리턴해야한다

대소문자는 무시한다

또, ['hello', 'hey'] 는 false를 리턴한다

hello에는 y 가 없기 때문이다

또, ['Alien', 'line']은 true를 리턴한다. Alien은 line을 모두 갖고 있기 때문이다

 

 

function mutation(arr) {
  for(let i of arr[1].toLowerCase()){
    if(!arr[0].toLowerCase().includes(i)){
      return false
    }
  }
  return true
}

mutation(["hello", "hey"]);

 

function getIndexToIns(arr, num) {
  arr.sort((a,b) => a-b)
  for(let i=0; i< arr.length; i++) {
    if(num <= arr[i]){
      return i
    }
  }
  return arr.length;
}

getIndexToIns([40, 60], 50);

Where do I Belong

 

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/where-do-i-belong

 

www.freecodecamp.org

 

주어진 arr를 먼저 정렬하고, 나서 

정렬 순서에 맞게 주어진 num 값이 주어진 arr에 추가 되어야 하는 인덱스 값, 

가장 낮은 인덱스를 리턴하라

리턴값은 숫자여야 한다.

 

예를 들어, getIndexToIns([1,2,3,4], 1.5) 를 실행하면 1이 리턴되어야한다.

왜냐하면 1.5는 1과 2 사이 이므로 인덱스 1을 리턴한다

똑같이, getIndexToIns([20,3,5], 19) 를 실행하면 2가 리턴되어야 한다.

왜냐하면, 정렬먼저하면 [3, 5, 20]이고, 19는 5와 20사이 에 들어가야 하므로 2가 리턴

 

 

Falsy Bouncer

 

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/falsy-bouncer

 

www.freecodecamp.org

 

 

모든 주어진 arr 에서 falsy 값을 지워라 

주어진 arr을 mutate - 변화시키지 마라 (유지해라)

js에서 falsy 값들은  false, null, 0, "", undefined, and NaN.

힌트 : 각각의 값을 boolean으로 바꿔봐라

 

function bouncer(arr) {
  let arr2 = []
  for(let i of arr){
    if(Boolean(i)){
      arr2.push(i)
    }
  }
  return arr2;
}

bouncer([7, "ate", "", false, 9]);

Slice and Splice

 

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice

 

www.freecodecamp.org

 

function frankenSplice(arr1, arr2, n) {
  let a2_1 = arr2.slice(0, n)
  let a2_2 = arr2.slice(n)
  let arr3 = [...a2_1, ...arr1, ...a2_2]

  return arr3;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);

 

Title Case a Sentence

 

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence

 

www.freecodecamp.org

 

function titleCase(str) {
  let arr = str.split(' ')
  let anw = []
  for (let i of arr){
    anw.push(i[0].toUpperCase() + i.slice(1).toLowerCase())
  }
  let w = anw.join(' ')
  return w;
}

titleCase("I'm a little tea pot");

Boo who

 

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/boo-who

 

www.freecodecamp.org

 

function booWho(bool) {
  if(typeof bool == 'boolean') return true
  return false
}

booWho(null);

 

Finders Keepers

 

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers

 

www.freecodecamp.org

 

function findElement(arr, func) {
  for (let a of arr){
    if(func(a)) return a
  }
}

findElement([1, 2, 3, 4], num => num % 2 === 0);

Truncate a String

 

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string

 

www.freecodecamp.org

 

function truncateString(str, num) {
  if(str.length > num) {
    return str.slice(0, num) + '...'
  }
  return str;
}

truncateString("A-tisket a-tasket A green and yellow basket", 8);

+ Recent posts