Advertisement
nher1625

basics

May 2nd, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var myHeading = document.querySelector('h1'); // grabs a reference to the heading specified by the string h1
  2. //myHeading.innerHTML = 'Hello world!';
  3.  
  4. // Comparing arrays to see if both are identical
  5. function compareEquality(a, b) {
  6.     if (a === b) return true;
  7.     if (a == null || b == null) return false;
  8.     if (a.length != b.length) return false;
  9.  
  10.     // If order of array elements is not important, sort them here.
  11.     for (var i =0; i < a.length; i++) {
  12.         if (a[i] !== b[i]) return false;
  13.     }
  14.     return true;
  15. };
  16.  
  17. var iceCream = "Strawberry";
  18.  
  19. function isFavFlav(flavor) {
  20.     var favorites = [ "coconut", "pineapple", "rocky road", "french vanilla", "blueberry" ];
  21.  
  22.     for (i = 0; i < favorites.length; i++) {
  23.         if (flavor === favorites[i]) {
  24.             var inCommon = "The flavor of " + favorites[i] + " is my favorite!";
  25.         }
  26.     };
  27.  
  28.     if (!inCommon) {
  29.         console.log("We won't get along since we have little in common.");
  30.     }
  31.     else { console.log(inCommon); }
  32. };
  33.  
  34. /* Events
  35. Events allow for the development of real on-site interactivity. Events are
  36. code structures that listen for things that happen to the browser, and then
  37. allow you to run code in response to those triggers.
  38.  */
  39.  
  40.  document.querySelector('h1').onclick = function() {
  41.     alert('Event triggered: <h1> element has been clicked.');
  42.  };
  43.  
  44.  // Image Changer
  45.  var myImage = document.querySelector('img');
  46.  
  47.  myImage.onclick = function() {
  48.     var myImageSource = myImage.getAttribute('src');
  49.     // Note that the images src attribute is retrieved once per click
  50.     if (myImageSource === "images/firefox-icon.png") {
  51.         myImage.setAttribute('src', 'images/firefox-icon-1.png');
  52.     } else {
  53.         myImage.setAttribute('src', 'images/firefox-icon.png');
  54.     }
  55.  };
  56.  
  57.  // Change User
  58.  var myButton = document.querySelector('button');
  59.  
  60.  function setUserName() {
  61.     var myName = prompt("Enter a Username: ");
  62.     localStorage.setItem('name', myName);
  63.     myHeading.innerHTML = 'Welcome, ' + myName;
  64.  };
  65.  
  66.  // Initialization Code
  67.  if(!localStorage.getItem('name')) {
  68.     setUserName();
  69.  } else {
  70.     var storedName = localStorage.getItem('name');
  71.     myHeading.innerHTML = "Welcome back, " + storedName;
  72.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement