|
| 1 | +<a id=top></a> |
| 2 | + |
| 3 | +<details> |
| 4 | + <summary> |
| 5 | + <h4>Description of the task</h4> |
| 6 | + </summary> |
| 7 | + <h3>Step 6</h3> |
| 8 | + <p> |
| 9 | + CamperBot has now created a function called <code>changeBackgroundColor</code> that changes the background color of the page to a random color from the <code>darkColorsArr</code> array. The function also displays the hex code for that new color. |
| 10 | + </p> |
| 11 | + <p> |
| 12 | + When they try to test out this function, they notice that the background color is not changing and the text shows the following: |
| 13 | + </p> |
| 14 | + <details> |
| 15 | + <summary> |
| 16 | + <h5>Example Code</h5> |
| 17 | + </summary> |
| 18 | + <code>Hex Code: undefined</code> |
| 19 | + </details> |
| 20 | + <p> |
| 21 | + <code>undefined</code> is showing up here because the <code>color</code> variable is not being set correctly. |
| 22 | + </p> |
| 23 | + <p> |
| 24 | + Fix the error in the <code>darkColorsArr[getRandomIndex]</code> line so that the color variable is set to a random color from the <code>darkColorsArr</code> array. |
| 25 | + </p> |
| 26 | +</details> |
| 27 | + |
| 28 | +# Random Background Color changer |
| 29 | + |
| 30 | +Debugging is the process of going through your code, finding any issues, and fixing them. |
| 31 | + |
| 32 | +In this project, you will help CamperBot build a random background color changer and help them find and fix errors. |
| 33 | + |
| 34 | +#### preview |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +| [index.html](#indexhtml) | [styles.css](#stylescss) | [script.js](#scriptjs) | |
| 39 | +| ------------------------ | ------------------------ | ---------------------- | |
| 40 | + |
| 41 | +### technologies |
| 42 | + |
| 43 | +<table> |
| 44 | + <thead> |
| 45 | + <tr> |
| 46 | + <th height=33 width=91>JavaScript</th> |
| 47 | + <th height=33 width=91>CSS</th> |
| 48 | + <th height=33 width=91>HTML</th> |
| 49 | + </tr> |
| 50 | + </thead> |
| 51 | + <tbody> |
| 52 | + <tr> |
| 53 | + <td height=33 width=91> |
| 54 | + <a href=https://ecma-international.org/publications-and-standards/standards/> |
| 55 | + <img src=https://github.com/AndriiKot/JS__Role_Playing_Game__FreeCodeCamp/blob/main/preview/icons/javascript-1.svg alt=JavaScript> |
| 56 | + </a> |
| 57 | + </td> |
| 58 | + <td height=33 width=91> |
| 59 | + <a href=https://www.w3.org/Style/CSS/> |
| 60 | + <img src=https://github.com/AndriiKot/JS__Role_Playing_Game__FreeCodeCamp/blob/main/preview/icons/css.svg alt=CSS> |
| 61 | + </a> |
| 62 | + </td> |
| 63 | + <td height=33 width=91> |
| 64 | + <a href=https://html.spec.whatwg.org/multipage/> |
| 65 | + <img src=https://github.com/AndriiKot/JS__Role_Playing_Game__FreeCodeCamp/blob/main/preview/icons/html.svg alt=HTML> |
| 66 | + </a> |
| 67 | + </td> |
| 68 | + </tr> |
| 69 | + </tbody> |
| 70 | +</table> |
| 71 | + |
| 72 | +[back to top](#top) |
| 73 | + |
| 74 | +### index.html |
| 75 | + |
| 76 | +```html |
| 77 | +<!DOCTYPE html> |
| 78 | +<html lang="en"> |
| 79 | + <head> |
| 80 | + <meta charset="UTF-8" /> |
| 81 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 82 | + <title>Build a random background color changer</title> |
| 83 | + <link rel="stylesheet" href="./styles.css" /> |
| 84 | + </head> |
| 85 | + <body> |
| 86 | + <h1>Random Background Color changer</h1> |
| 87 | + |
| 88 | + <main> |
| 89 | + <section class="bg-information-container"> |
| 90 | + <p>Hex Code: <span id="bg-hex-code">#110815</span></p> |
| 91 | + </section> |
| 92 | + |
| 93 | + <button class="btn" id="btn">Change Background Color</button> |
| 94 | + </main> |
| 95 | + <script src="./script.js"></script> |
| 96 | + </body> |
| 97 | +</html> |
| 98 | +``` |
| 99 | + |
| 100 | +[back to top](#top) |
| 101 | + |
| 102 | +### styles.css |
| 103 | + |
| 104 | +```css |
| 105 | +*, |
| 106 | +*::before, |
| 107 | +*::after { |
| 108 | + margin: 0; |
| 109 | + padding: 0; |
| 110 | + box-sizing: border-box; |
| 111 | +} |
| 112 | + |
| 113 | +:root { |
| 114 | + --yellow: #f1be32; |
| 115 | + --golden-yellow: #feac32; |
| 116 | + --dark-purple: #110815; |
| 117 | + --light-grey: #efefef; |
| 118 | +} |
| 119 | + |
| 120 | +body { |
| 121 | + background-color: var(--dark-purple); |
| 122 | + color: var(--light-grey); |
| 123 | + text-align: center; |
| 124 | +} |
| 125 | + |
| 126 | +.bg-information-container { |
| 127 | + margin: 15px 0 25px; |
| 128 | + font-size: 1.2rem; |
| 129 | +} |
| 130 | + |
| 131 | +.btn { |
| 132 | + cursor: pointer; |
| 133 | + padding: 10px; |
| 134 | + margin: 10px; |
| 135 | + color: var(--dark-purple); |
| 136 | + background-color: var(--golden-yellow); |
| 137 | + background-image: linear-gradient(#fecc4c, #ffac33); |
| 138 | + border-color: var(--golden-yellow); |
| 139 | + border-width: 3px; |
| 140 | +} |
| 141 | + |
| 142 | +.btn:hover { |
| 143 | + background-image: linear-gradient(#ffcc4c, #f89808); |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +[back to top](#top) |
| 148 | + |
| 149 | +### script.js |
| 150 | + |
| 151 | +```js |
| 152 | +const darkColorsArr = [ |
| 153 | + "#2C3E50", |
| 154 | + "#34495E", |
| 155 | + "#2C2C2C", |
| 156 | + "#616A6B", |
| 157 | + "#4A235A", |
| 158 | + "#2F4F4F", |
| 159 | + "#0E4B5A", |
| 160 | + "#36454F", |
| 161 | + "#2C3E50", |
| 162 | + "#800020", |
| 163 | +]; |
| 164 | + |
| 165 | +function getRandomIndex() { |
| 166 | + console.log(Math.floor(darkColorsArr.length * Math.random())); |
| 167 | +} |
| 168 | + |
| 169 | +getRandomIndex(); |
| 170 | + |
| 171 | +const body = document.querySelector("body"); |
| 172 | +const bgHexCodeSpanElement = document.querySelector("#bg-hex-code"); |
| 173 | + |
| 174 | +console.log(bgHexCodeSpanElement); |
| 175 | +``` |
| 176 | + |
| 177 | +[back to top](#top) |
0 commit comments