Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>あなたの毛診断</title>
    <link rel="stylesheet" href="assessment.css">
</head>
<body>
    <h1>あなたの毛は?</h1>
    <p>診断したい名前を入れてください</p>
    <input type="text" id ="user-name" size ="40" maxlength ="20">
    <button id = "assessment">診断する</button>
    <div id ="result-area"></div>
    <div id ="tweet-area"></div>
    <script src="assessment.js"></script>
</body>
</html>
              
            
!

CSS

              
                body {
    background-color: #04A6EB;
    color: #FDFFFF;
    width: 500px;
    margin-right: auto;
    margin-left : auto;
}

button {
    padding: 5px 20px;
    background-color: #337AB7;
    border-color: #2E6DA4;
    border-style: none;
    color: #FDFFFF;
}

input {
    height: 20px;
}
              
            
!

JS

              
                (function () {
    'use strict'
    const userNameInput = document.getElementById('user-name');
    const assessmentButton = document.getElementById('assessment');
    const resultDivided = document.getElementById('result-area');
    const tweetDivided = document.getElementById('tweet-area');

    function removeAllChildren(element){
        while(element.firstChild){
            element.removeChild(element.firstChild);
        }
    }
    assessmentButton.onclick = () => {
        const userName = userNameInput.value;
        if (userName.length === 0) {
            return;
        }

        removeAllChildren(resultDivided);
        const header = document.createElement('h3');
        header.innerText = '診断結果';
        resultDivided.appendChild(header);
        
        removeAllChildren(tweetDivided);
        const paragraph = document.createElement('p');
        const result = assessment(userName);
        paragraph.innerText = result;
        resultDivided.appendChild(paragraph);

    }
    const answers = [
        '{userName}は髪の毛です。',
        '{userName}は眉毛です。',
        '{userName}は髭です。',
        '{userName}は睫毛です。',
        '{userName}は胸毛です。',
        '{userName}は腹毛です。',
        '{userName}は陰毛です。',
        '{userName}は尻毛です。',
        '{userName}は太腿毛です。',
        '{userName}は背中毛です。',
        '{userName}は腋毛です。',
        '{userName}は耳毛です。',
        '{userName}は鼻毛です。',
        '{userName}は指毛です。',
        '{userName}は毛むくじゃらです。',
        '{userName}は禿です。'
    ];

    /**
* 名前の文字列を渡すと診断結果を返す関数
* @param {string} userName ユーザーの名前
* @return {string} 診断結果
*/
    function assessment(userName) {
        let sumOfcharCode = 0;
        for (let i = 0; i < userName.length; i++) {
            sumOfcharCode = sumOfcharCode + userName.charCodeAt(i);
        }
        const index = sumOfcharCode % answers.length;
        let result = answers[index];
        result = result.replace(/\{userName\}/g, userName);
        return result;
    }
    console.assert(
        assessment('太郎') === assessment('太郎'),
        '入力が同じ名前なら同じ診断結果を出力する処理が正しくありません。'
    );
})();
              
            
!
999px

Console