FAQ: Languages for Web Development - JavaScript Functions

This community-built FAQ covers the “JavaScript Functions” exercise from the lesson “Languages for Web Development”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Code Foundations

FAQs on the exercise JavaScript Functions

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In step 3:

After clicking the repaint button, the colors change but the step won’t be checked, and it’s greyed out, so I can’t move to the next lesson.

This issue happen to me on both Chrome and Firefox.

click the run button, the next button should be available then if not paste in the error and your code.

2 Likes

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?

I have been the same problem and when I clicked on “run” button it worked.

Yes, it’s called camelCase and this post from another course might help.

Quick question.

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.

4 Likes

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! :slight_smile:

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>
1 Like

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 :wink:
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.

1 Like

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

when I paste onclick=“changeColor()” into the opening tag of button element, it doesn’t change anything and letting me move on to the next step

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.

1 Like

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?

follow the hint given there itself in screen in instruction section how to put code there.

just follow the hint i m sure you have mistaken something

didnt get your comment, can you explain it in simple terms??

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.

I’m new here though

Thank you