I need help with JavaScript

So I’m taking the JavaScript course and it’s time to do excerise. I’m copying everything Ivan is doing, but the rows aren’t populating on website. Someone please help me wish I could upload a pic to show you.

2 Likes

You can upload a picture here, but it would be better if you could paste the code here, so we can easily test out what’s wrong with the code.

Here is how to post your code. Copy the code and paste it in the reply box. After that highlight the code with your mouse and click the button on the picture below to properly display your HTML code.

prefromatted_text-animated

4 Likes

Which exercise are you stuck on? You can load a picture by using the buttons above.

3 Likes
  for (let line = "#"; line.lenght < 8; line+= "#"){
                    console.log(line);
                  }

this isn’t working! one of the exercises that doesn’t populate…

1 Like
Trav website wuwu
<body>
      <h1> Great website Travvy dopre </h1>

      <script>
          var num_rows = 7;
          for(var row = 0; row < num_rows; row++){
            console.log("row number" + row);
          }



      </script>

</body>
1 Like

Good morning, so above are the codes that are not populating for me. when i do the exercise in javascript. i’ve learned about the loops & boolean. which isn’t easy to really decipher. When i follow ivan video on this above code i get nothing.

1 Like

Hi Travis, when I tried to copy the last code snippet I got it to work… I was so happy, because I can help you a little. :hugs:

Ok. I try to explain what I did, the best I can.

I copied your code snippet like this below to test it in my browser console.
I copied the snippet without the <script> tag.

var num_rows = 7;
          for(var row = 0; row < num_rows; row++){
            console.log("row number" + row);
          }

then I copied it into my browser and I got this result in my console.


Congratulations. Now we know that It works… :partying_face::rocket: that’s fantastic.
Could you please try the same in your browser and see if it works in your browser?

I like to use the sand-box when I test my own code and student code. I have posted the link here for your convenience. Sand-box

Ivo

2 Likes

Awesome, thanks! so i took off the script tag as suggested. But few questions here

  1. why is it that it works without tag? is that normal?
  2. when i run code in atom without script tag look at this screen shot. The code written comes out as is.
  3. In the video ivan did he did the code in atom. Think you have yours written within the website using the console.
  4. when i did the code on console. it worked perfect.

Hi everyone,

I have a quick question regarding scope in jQuery when working my solution from the “Getting User Input - Text” assignment. I defined var bla globally, which I realized was the problem but I just would like to know why the jQuery function was not able to access the input value from a global variable as follows:

<body>

  <input id="textInput" type="text"/>
  <button id="butt">Submit</button>

  <script>

  var bla = $("#textInput").val();

  $("#butt").click(function(){
    alert(bla);
  });
  </script>

</body>
1 Like

Hey @Cliff!

Great question!
The issue here is that when the program executes and loads the page, you haven’t entered anything into the text box yet. So your var bla variable will store an “empty” value because that’s what $("#textInput").val() represents at that particular time.

The click function is an event listener — it doesn’t execute when the page loads, but is a lurker in the dark, waiting and “listening” for the click event, and only when it “hears” the click does it spring into action…

So, with your code, when you enter a fruit and click submit, the click function runs, but when alert() calls bla , the value passed to it is still the empty one. That’s why the alert pop-up isn’t displaying the fruit you input. The fact that the click function is written using jQuery syntax has nothing to do with the issue: the function can access the variable outside its local scope; it’s just that the value it accesses is an “empty” one.

Instead, when you place the variable within the click function, and above alert(), it isn’t assigned anything until the “flow” of our program reaches it after the click event. And at that point there is a value in the text box for it to “grab” with its tentacle (you click submit after you input your fruit).

There is another alternative if you want to declare bla outside the function, and that is as follows:

<script>

  var bla = $("#textInput");

  $("#butt").click(function(){
    alert(bla.val());
  });
   
</script>

Do you see why that does work?

3 Likes

Thank you! It’s a relief to know that scope works as expected with jQuery!
My understanding of your example goes like this:
Var “bla” binds to the input div itself. The value property is accessed only when the click events occurs.

2 Likes

You’ve got it :+1:
var bla binds to the input element which stays constant from the page load. Then, as you say, we can access its value with .val() when the click event occurs by which time the value is something meaningful (i.e. a fruit we’ve just input).

2 Likes