Skip to content

Instantly share code, notes, and snippets.

@Ifycode
Created August 1, 2020 03:22

Revisions

  1. Ifycode created this gist Aug 1, 2020.
    26 changes: 26 additions & 0 deletions app.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    'use strict';

    const hex = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F'];
    const section1 = document.querySelector('.section1');
    const btn = document.getElementById('btn');
    const color = document.querySelector('.color');

    let getRandomNumber = () => {
    return Math.floor(Math.random()*hex.length);
    }

    const changeColor = () => {
    let hexColor = '#';
    let i = 0;
    for (; i < 6; i++) {
    hexColor += hex[getRandomNumber()];
    }
    color.innerHTML = hexColor;
    color.style.color = hexColor;
    section1.style.backgroundColor = hexColor;
    }

    changeColor(); //on js file load

    btn.addEventListener('click', changeColor); //on click