Why are functions case sensitive? Is it always the first word lowercase and proceeding are capitalized? Is there a reason or logic behind this beyond this is how it is?
Can someone please explain the function getRandomColor line by line, especially the line where we change the color variable in the “for” loop? I have a hard time understanding how a random color is created.
So I have this concern. I have only completed the code foundations course, learning about the basics of programming and coding, such as variables, loops, lists, functions, etc. I am now on this course seeing an overview of the Web Development languages used… I feel scared, I feel I am just that dumb that I cannot make that much sense of the code below. For example, where it says i=0 and i<6. I am guessing that’s because of the 00 00 00 digit for color code, but it is not explained if that is the case.
Then where it says return color, I know it is to return a value, but my point is that only after seeing these examples, I feel like I am not confident to start writing my own code. Did you guys feel like you could write your own code after only these lessons?
Perhaps I’m just exaggerating, but I want to know if at any point during the courses the website actually explains the details in the code, for example, why the let ="#" equals that. Or can you define a function before or after a certain part of your code?? Or Is there an issue if you hit the “space” button within a Tag? I feel dumb for asking this, but the only way is up, that is, learning.
Let me know if any of you feel the same way. Maybe we could help each other!
Paint Me!
<script>
function getRandomColor(){
let letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function changeColor(){
let newColor = getRandomColor();
document.body.style.backgroundColor = newColor;
}
</script>
Hello, I seem to have placed the code in the right place because it’s letting me move on however when I hit Repaint!, the color doesnt change?
Repaint!
<script>
function getRandomColor()
let letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() *
Hi! not sure this is where I ask question related to this lesson so feel free to let me know if it isn’t
In this lesson we saw how the onclick event executed the changecolor() function.
My question is, since the script is under the button tag, shouldn’t the website run into an error since it will first load the button, look for the function that doesn’t exist yet and only then load it?
Unless I am mistaken (in which case hopefully someone will correct me), the function will only be needed once the button is clicked. When the button is rendered (but we haven’t loaded the script yet), then the onClick attribute will just know that if the button is clicked, it should call a function named changeColor. But it won’t bother checking whether such a function even exists. Only when the button is clicked, then it will make a call to the function. At this point, it will need to know whether a function named changeColor can be reached.
Suppose instead of
<button onClick="changeColor()">
we had a typo,
<button onClick="changeCoor()">
the page will load without any error showing up in the console. When you click the button, then a ReferenceError will show that changeCoor is not defined.
Ah I see that makes sense
Thanks for responding ! a follow up question though:
I assume this means that the HTML elements only load after the entire code has been 'rendered"? (I’m not sure about the correct term) so that only after the script is loaded it shows the button and the user can click it
Well, it depends on the definition of the variable (i) at this case. The formula is for not covering the letters with the results I guess, it goes with the hexadecimal but i don’t know what is the command (floor). Thanks.
Step by step, using it you will understand, im not confident at all but each formula relates to other that you 've previously seen mostly, of course there’s always some to discover.
Good luck and keep going! Thanks for your contribution.
Instead of using random colour for changing the page you can use a library with images that are close to the median of the corpus and it will creates an ilusion of knowledge of the script, right? Like a closed corpus of frequent thoughts, you can amplify the answers with some pictures from the mode for the distribution.
You just need a closed question button. Like… Dou you like this beach?
This happened to me too, i don’t know why? I also compared my solution with codecademy solution but it seems pretty the same. i don’t see any button on my browser and no color was displayed.