Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. // GWHS - 12/22 Handout
  2.  
  3. // This is a one line javascript comment
  4. /* And so is this one,
  5. But it can go many lines */
  6.  
  7. // Takes no parameters and returns nothing
  8. function sayHi() {
  9. console.log("Hi!");
  10. }
  11.  
  12. sayHi();
  13. /* Answer:
  14. Hi!
  15. */
  16.  
  17.  
  18. // Takes no parameters but returns something
  19. function getHelloWorld() {
  20. return "Hello world.";
  21. }
  22.  
  23. console.log(getHelloWorld());
  24. /* Answer:
  25. Hello world.
  26. */
  27.  
  28.  
  29. // Takes one parameter and returns nothing
  30. function logMyName(name) {
  31. console.log(name);
  32. }
  33.  
  34. logMyName("Chris");
  35. /* Answer:
  36. Chris
  37. */
  38.  
  39.  
  40. // Takes one parameter and returns a new string
  41. function returnMyExcitingName(name) {
  42. // We can use the plus sign to add strings together
  43. return name + "!";
  44. }
  45.  
  46. console.log(returnMyExcitingName("Christopher"));
  47. /* Answer:
  48. Christopher!
  49. */
  50.  
  51. // Takes two parameters and returns a new string
  52. function returnMyNameAndAge(name, age) {
  53. return "I am " + name + " and I am " + age;
  54. }
  55.  
  56. console.log(returnMyNameAndAge("Christopher", 33));
  57. /* Answer:
  58. I am Christopher and I am 33
  59. */
  60.  
  61.  
  62. // This function uses some of our other functions
  63. function logMyAgeAndNameExclaimed(myName) {
  64. // We can make a new variable that starts as an empty string
  65. var newString = '';
  66. /* This next line is important to understand -
  67. make sure you can explain it or ask for clarification */
  68.  
  69. newString = returnMyNameAndAge(returnMyExcitingName(myName), 33.5);
  70. console.log(newString);
  71. }
  72.  
  73. logMyAgeAndNameExclaimed("Jesse");
  74. /* Answer:
  75. I am Jesse! and I am 33.5
  76. */
  77.  
  78.  
  79. // This function takes one parameter (also can be called an argument)
  80. // And uses an if statement in it’s body
  81. function isMyAgeOld(age) {
  82. if (age < 30) {
  83. return "You’re a youngster!";
  84. } else {
  85. return "You guys like MySpace?!";
  86. }
  87. }
  88.  
  89. var age = 27;
  90. console.log(isMyAgeOld(age));
  91. /* Answer:
  92. You’re a youngster!
  93. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement