[JavaScript30] πŸ—ƒ 17. Sort Without Articles

μ‘°μ€€ν˜•Β·2021λ…„ 7μ›” 11일
0

JavaScript30

λͺ©λ‘ 보기
17/30

πŸ—ƒ 17. Sort Without Articles

λ°°μ—΄μ˜ λ‚΄μš©μ—μ„œ 뢀사(a, the, an)을 제거 ν•˜κ³  μ•ŒνŒŒλ²³ 순으둜 μž¬λ°°μ—΄.

μ΄ˆκΈ°μ½”λ“œ

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sort Without Articles</title>
</head>
<body>
    
    <style>
        body {
        margin: 0;
        font-family: sans-serif;
        background: url("https://source.unsplash.com/nDqA4d5NL0k/2000x2000");
        background-size: cover;
        display: flex;
        align-items: center;
        min-height: 100vh;
        }

        #bands {
        list-style: inside square;
        font-size: 20px;
        background: white;
        width: 500px;
        margin: auto;
        padding: 0;
        box-shadow: 0 0 0 20px rgba(0, 0, 0, 0.05);
        }
        
        #bands li {
        border-bottom: 1px solid #efefef;
        padding: 20px;
        }
        
        #bands li:last-child {
        border-bottom: 0;
        }

        a {
        color: #ffc600;
        text-decoration: none;
        }

    </style>

    <ul id="bands"></ul>

<script>
const bands = ['The Plot in You', 'The Devil Wears Prada', 'Pierce the Veil', 'Norma Jean', 'The Bled', 'Say Anything', 'The Midway State', 'We Came as Romans', 'Counterparts', 'Oh, Sleeper', 'A Skylit Drive', 'Anywhere But Here', 'An Old Dog'];


</script>

</body>
</html>

μ΄ˆκΈ°ν™”λ©΄

🌏 κ³Όμ •

πŸ‘‰ 1. μ •λ ¬

const sortedBands = bands.sort((a,b) => {
    if(strip(a) > strip(b)){
    	return 1;
    } else{
    	return -1;
    }
});

μœ„ μ½”λ“œλ₯Ό 쀄이기.

β‘  3ν•­μ—°μ‚°μž μ‚¬μš©.

const sortedBands = bands.sort((a,b)=>{
    return strip(a) > strip(b) ? 1 : -1;
})

β‘‘ ES6 Arrow function

const sortedBands = bands.sort((a,b) => strip(a) > strip(b) ? 1 : -1);

πŸ‘‰ 2. 뢀사λ₯Ό μ œκ±°ν•  ν•¨μˆ˜ 생성

function strip(bandName){
    return bandName.replace(/^(a |the |an )/i, '').trim();
}

a, the, an이 있으면 ''둜 λ³€κ²½ ν›„ trim으둜 μž˜λΌλƒ„.

πŸ‘‰ 3. λ¬Έμ„œμ— 맀핑

document.querySelector('#bands').innerHTML = sortedBands
    .map(band => `<li>${band}</li>`)
    .join('');

idκ°€ band인 νƒœκ·Έμ•ˆμ— map()μ•ˆμ˜ λ‚΄μš©μ„ λ„£λŠ”λ‹€.

profile
κΉƒν—ˆλΈŒ : github.com/JuneHyung

0개의 λŒ“κΈ€