Skip to content

Commit de0b50a

Browse files
committed
'Random Background Color Changer' fix: darkColorsArr[] (step 6)
1 parent da25b2b commit de0b50a

4 files changed

Lines changed: 78 additions & 13 deletions

File tree

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,29 @@ In this project, you will help CamperBot build a random background color changer
114114
Fix the <code>document.querySelector("bg-hex-code")</code> line so that it correctly selects the element with the <code>id</code> of <code>bg-hex-code</code>.
115115
</p>
116116
</details>
117+
<details>
118+
<summary>
119+
<h5>Step 6</h5>
120+
</summary>
121+
<p>
122+
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.
123+
</p>
124+
<p>
125+
When they try to test out this function, they notice that the background color is not changing and the text shows the following:
126+
</p>
127+
<details>
128+
<summary>
129+
<h5>Example Code</h5>
130+
</summary>
131+
<code>Hex Code: undefined</code>
132+
</details>
133+
<p>
134+
<code>undefined</code> is showing up here because the <code>color</code> variable is not being set correctly.
135+
</p>
136+
<p>
137+
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.
138+
</p>
139+
</details>
117140
</details>
118141

119142
#### preview
@@ -248,15 +271,21 @@ const darkColorsArr = [
248271
];
249272

250273
function getRandomIndex() {
251-
console.log(Math.floor(darkColorsArr.length * Math.random()));
274+
const randomIndex = Math.floor(darkColorsArr.length * Math.random());
275+
return randomIndex;
252276
}
253277

254-
getRandomIndex();
255-
256278
const body = document.querySelector("body");
257279
const bgHexCodeSpanElement = document.querySelector("#bg-hex-code");
258280

259-
console.log(bgHexCodeSpanElement);
281+
function changeBackgroundColor() {
282+
const color = darkColorsArr[getRandomIndex()];
283+
284+
bgHexCodeSpanElement.innerText = color;
285+
body.style.backgroundColor = color;
286+
}
287+
288+
changeBackgroundColor();
260289
```
261290

262291
[back to top](#top)

__0__title__/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,32 @@ In this project, you will help CamperBot build a random background color changer
114114
Fix the <code>document.querySelector("bg-hex-code")</code> line so that it correctly selects the element with the <code>id</code> of <code>bg-hex-code</code>.
115115
</p>
116116
</details>
117+
<details>
118+
<summary>
119+
<h5>Step 6</h5>
120+
</summary>
121+
<p>
122+
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.
123+
</p>
124+
<p>
125+
When they try to test out this function, they notice that the background color is not changing and the text shows the following:
126+
</p>
127+
<details>
128+
<summary>
129+
<h5>Example Code</h5>
130+
</summary>
131+
<code>Hex Code: undefined</code>
132+
</details>
133+
<p>
134+
<code>undefined</code> is showing up here because the <code>color</code> variable is not being set correctly.
135+
</p>
136+
<p>
137+
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.
138+
</p>
139+
</details>
117140
</details>
118141

142+
119143
### technologies
120144

121145
<table>

__6__step__/README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ In this project, you will help CamperBot build a random background color changer
3333

3434
#### preview
3535

36-
![preview 'Random Background Color Changer step 5'](https://github.com/AndriiKot/JS__Random_Background_Color_Changer__freeCodeCamp/blob/main/preview/step1.png)
36+
![preview 'Random Background Color Changer step 6'](https://github.com/AndriiKot/JS__Random_Background_Color_Changer__freeCodeCamp/blob/main/preview/step1.png)
3737

3838
| [index.html](#indexhtml) | [styles.css](#stylescss) | [script.js](#scriptjs) |
3939
| ------------------------ | ------------------------ | ---------------------- |
@@ -163,15 +163,21 @@ const darkColorsArr = [
163163
];
164164

165165
function getRandomIndex() {
166-
console.log(Math.floor(darkColorsArr.length * Math.random()));
166+
const randomIndex = Math.floor(darkColorsArr.length * Math.random());
167+
return randomIndex;
167168
}
168169

169-
getRandomIndex();
170-
171170
const body = document.querySelector("body");
172171
const bgHexCodeSpanElement = document.querySelector("#bg-hex-code");
173172

174-
console.log(bgHexCodeSpanElement);
173+
function changeBackgroundColor() {
174+
const color = darkColorsArr[getRandomIndex()];
175+
176+
bgHexCodeSpanElement.innerText = color;
177+
body.style.backgroundColor = color;
178+
}
179+
180+
changeBackgroundColor();
175181
```
176182

177183
[back to top](#top)

__6__step__/script.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ const darkColorsArr = [
1212
];
1313

1414
function getRandomIndex() {
15-
console.log(Math.floor(darkColorsArr.length * Math.random()));
15+
const randomIndex = Math.floor(darkColorsArr.length * Math.random());
16+
return randomIndex;
1617
}
1718

18-
getRandomIndex();
19-
2019
const body = document.querySelector("body");
2120
const bgHexCodeSpanElement = document.querySelector("#bg-hex-code");
2221

23-
console.log(bgHexCodeSpanElement);
22+
function changeBackgroundColor() {
23+
const color = darkColorsArr[getRandomIndex()];
24+
25+
bgHexCodeSpanElement.innerText = color;
26+
body.style.backgroundColor = color;
27+
}
28+
29+
changeBackgroundColor();

0 commit comments

Comments
 (0)