[ javascript ] 유용한 함수들 10가지 > javascript&jQuery

본문 바로가기

사이트 내 전체검색

javascript&jQuery

[ javascript ] 유용한 함수들 10가지

작성일 21-02-15 22:58

페이지 정보

작성자 웹지기 조회 3,373회 댓글 0건

본문

javascript 유용한 함수들 10가지

https://youtu.be/3CUjtKJ7PJg

 

// Q1. make a string out of an array

{

    const fruits = ['apple', 'banana', 'orange'];

    const result = fruits.join();

    console.log(result);

}

  

// Q2. make an array out of a string

{

    const fruits = 'apple, banana, melon, tomato';

    const result = fruits.split(",");

    console.log(result);

}


// Q3. make this array look like this: [5, 4, 3, 2, 1]

{

    const array = [1, 2, 3, 4, 5];

    const arrSort = array.reverse();

    console.log(arrSort);

    console.log(array);

}


// Q4. make new array without the first two elements

{

    const array = [1, 2, 3, 4, 5];

    const result = array.splice(2, 5);

    console.log(result);

    console.log(array);

}


class Student {

    constructor(name, age, enrolled, score) {

        this.name = name;

        this.age = age;

        this.enrolled = enrolled;

        this.score = score;

    }

}

const students = [

    new Student('A', 29, true, 45),

    new Student('B', 28, false, 80),

    new Student('C', 30, true, 90),

    new Student('D', 40, false, 66),

    new Student('E', 18, true, 88),

];


// Q5. find a student with the score 90

{

    console.clear();

    /*

    * 해당하는 배열만 찾아 낼 때 find()

    const result = students.find(function(student, index){

        //console.log(student, index);

        return student.score === 90;

    });

    */

    //arrow function expression 으로 변경

    const result = students.find((student) => student.score === 90);

    console.log(result);

}


// Q6. make an array of enrolled students

{

    console.clear();

    /*

    * 조건에 맞는 배열만을 가져오고 싶을 때 filter()

    const result = students.filter(function(student){

        //console.log(student);

        return student.enrolled === true;

    });

    */

    //arrow function expression 으로 변경

    const result = students.filter((student) => student.enrolled);

    console.log(result);

}


// Q7. make an array containing only the students' scores

// result should be: [45, 80, 90, 66, 88]

{

    console.clear();

    /*

    * 학생들의 점수만을 새로운 배열로 넣고 싶을 때 map()

    const result = students.map(function(student){

    //점수를 두배로 바꾸고 싶다면 return student.score * 2를 해주면 된다.

        return student.score;

    });

    */

    //arrow funtion expression 으로 변경

    const result = students.map((student) => student.score);

    console.log(result);

}


// Q8. check if there is a student with the score lower than 50

{

    console.clear();

    //배열의 조건이 만족할 때 출력하고 싶다면 some()

    /*

    const result = students.some(function(student) {

        return student.score < 50;

    });

    */

    //arrow function expression

    const result = students.some((student) => student.score < 50);

    console.log(result);


    //모든 배열의 조건이 만족하게 하고 싶다면 every()

    //some()처럼 사용하고 싶으면 !을 사용하면 된다.

    //const result2 = !students.every(function(student) {

    //    return student.score >= 50;

    //});

    /*

    * 이값은 모든 조건이 50보다 작은값이 아니므로 false

    const result2 = students.every(function(student) {

        return student.score < 50;

    });

    */

    //arrow function expression

    const result2 = !students.every((student) => student >= 50);

    console.log(result2);

}


// Q9. compute students' average score

{

    console.clear();

    //reduce는 어떤값을 돌면서 누적할 때 사용

    //reduceRight는 뒤에서 부터 반복해서 누적할 때 사용

    //reduce는 curr에 배열이 하나 하나씩 순차적으로 전달이 된다.

    //prev는 처음에는 배열의 첫번째값 두번째부터는 return 되어 오는 값을 받아 들인다.

    /*

    const result = students.reduce(function(prev, curr) {

        //console.log('------------');

        //console.log(prev);

        //console.log(curr);

        //return curr; 값의 전달 확인용

        //점수의 합을 구하므로 아래처럼 합을 해준다.

        return prev + curr.score;

    }, 0);

    */

    //arrow function expression

    const result = students.reduce((prev, curr) => prev+curr.score, 0);

    console.log(result/students.length);

}


// Q10. make a string containing all the scores

// result should be: '45, 80, 90, 66, 88'

{

    //점수에 대한 새로운 배열을 만든다.

    const result = students

        .map((student) => student.score) //학생들을 점수로 변환하고

        .filter((score) => score >= 50) //점수 50점 이상인 학생만 필터하고

        .join(); //조인을 해주면 된다.

    console.log(result);

}


// Bonus! do Q10 sorted in ascending order

// result should be: '45, 66, 80, 88, 90'

{

    const result = students

        .map((student) => student.score)

        .sort((a, b) => a - b ) //낮은 점수 먼저 부여주고 높은점수 부터 보고 싶다면 b-a로 수정하면된다.

        .join();

        console.log(result);

}

 


추천0

비추천 0

댓글목록

등록된 댓글이 없습니다.

전체 63건 1 페이지

이미지 목록

게시물 검색
Copyright © 즐거운 코딩 생활 ( funyphp ). All rights reserved.
PC 버전으로 보기