Jump to content

Allerious

Members
  • Content Count

    7
  • Joined

  • Last visited

Everything posted by Allerious

  1. It's my understanding that we need to run this line of code at the beginning of the script (see link) so that the GamePiece variables are global and thus allowing the updateGameArea() function to use them. If i remove this code the updateGameArea() still runs those objects as though they're global. Surely they're local to the startGame() function by removing this first line of code? var redGamePiece, blueGamePiece, yellowGamePiece; https://www.w3schools.com/graphics/tryit.asp?filename=trygame_component_more
  2. Allerious

    AJAX

    The code i've supplied is from this link: https://www.w3schools.com/js/tryit.asp?filename=tryjs_ajax_first The only difference being that i've removed the 'xhttp.onreadystatechange' function creation. Does 'responseText' in my code not output anything because at the point that piece of code is run the response hasn't been received from the server yet? I'm thinking maybe there's a slight delay hence the need for the piece of code i removed i.e the removed code is needed as it's only run once the response is received from the server i.e when the 'readyState' and 'status' changes. I assume that is why my code doesn't run with that piece of code removed but wanted to be sure. <!DOCTYPE html> <html> <body> <h2>The XMLHttpRequest Object</h2> <p id="demo">Let AJAX change this text.</p> <button type="button" onclick="loadDoc()">Change Content</button> <script> loadDoc(){ var xhttp = new XMLHttpRequest(); xhttp.open("GET", "ajax_info.txt", true); xhttp.send(); document.getElementById("demo").innerHTML = xhttp.responseText; } </script> </body> </html>
  3. When i run the code below it outputs: object function function function I don't understand why 'Array' is being output as 'function'. Is it not an object? 'Array' is the object and 'Array.isArray' is the method/function of that object. <!DOCTYPE html> <html> <body> <p id="demo1"></p> <p id="demo2"></p> <p id="demo3"></p> <p id="demo4"></p> <script> var myObj = { name: "bob", age: 50, nameAge : function() { return this.name + " " + this.age; } } var x = document.getElementById("demo1"); x.innerHTML = typeof myObj; var x = document.getElementById("demo2"); x.innerHTML = typeof myObj.nameAge; var x = document.getElementById("demo3"); x.innerHTML = typeof Array; var x = document.getElementById("demo4"); x.innerHTML = typeof Array.isArray; </script> </body> </html> Maybe 'isArray' is a function nested within the 'Array' function? If this is the case what would the syntax be for creating this? For example how would i change the following so myFun.withinFun() outputs "test" function myFun(){ function withinFun(){return "test"} }
  4. Is there any use/need/benefit from attaching window. before a global variable? In this first link it shows window.carName can be used, but removing window. from carName gives the same result, so is there ever any need for attaching window. at the beginning of global variables? https://www.w3schools.com/js/tryit.asp?filename=tryjs_let_scope This second link shows by using let instead of var for the variable creation window.carName is undefined yet it says in the green comments // code here can use window.carName. I assume that is a typing error and should read // code here can not use window.carName? https://www.w3schools.com/js/tryit.asp?filename=tryjs_let_global
  5. I've tried to replicate the bulb on/off code using cards. I've copy/pasted the w3 code and change the image names to my own and it doesn't work. Can someone spot what is wrong here? I've attached a screen shot of what appears on my screen. The code for the small images of the card back and ace of spades are there just to show i'm using the correct image name/location. The idea is when i click the large card back image it should turn to the ace of spades but doesn't. https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_state_if_else2 <!DOCTYPE html> <html> <head> </head> <body> <img src="images/card back.jpg" width="50" height="50"> <img src="images/Ace Spades.png" width="50" height="50"> <img id="myImage" onclick="changeImage()" src="images/card back.jpg" width="100" height="180"> <script> function changeImage() { var image = document.getElementById("myImage"); if (image.src.match("card back")) { image.src = "images/Ace Spades.png"; } else { image.src = "images/card back.jpg"; } } </script> </body> </html>
  6. Hello I'm starting the javascript tutorial and wondered what the purpose of window in window.alert() is? If i remove window it does the same thing i.e an alert box appears at the top of the page. My guess is alert() is window by default and there are other things that can be used in place of window to make the alert appear differently. Is this correct?
×
×
  • Create New...