Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // GWHS - 12/22 Handout
- // This is a one line javascript comment
- /* And so is this one,
- But it can go many lines */
- // Takes no parameters and returns nothing
- function sayHi() {
- console.log("Hi!");
- }
- sayHi();
- /* Answer:
- Hi!
- */
- // Takes no parameters but returns something
- function getHelloWorld() {
- return "Hello world.";
- }
- console.log(getHelloWorld());
- /* Answer:
- Hello world.
- */
- // Takes one parameter and returns nothing
- function logMyName(name) {
- console.log(name);
- }
- logMyName("Chris");
- /* Answer:
- Chris
- */
- // Takes one parameter and returns a new string
- function returnMyExcitingName(name) {
- // We can use the plus sign to add strings together
- return name + "!";
- }
- console.log(returnMyExcitingName("Christopher"));
- /* Answer:
- Christopher!
- */
- // Takes two parameters and returns a new string
- function returnMyNameAndAge(name, age) {
- return "I am " + name + " and I am " + age;
- }
- console.log(returnMyNameAndAge("Christopher", 33));
- /* Answer:
- I am Christopher and I am 33
- */
- // This function uses some of our other functions
- function logMyAgeAndNameExclaimed(myName) {
- // We can make a new variable that starts as an empty string
- var newString = '';
- /* This next line is important to understand -
- make sure you can explain it or ask for clarification */
- newString = returnMyNameAndAge(returnMyExcitingName(myName), 33.5);
- console.log(newString);
- }
- logMyAgeAndNameExclaimed("Jesse");
- /* Answer:
- I am Jesse! and I am 33.5
- */
- // This function takes one parameter (also can be called an argument)
- // And uses an if statement in it’s body
- function isMyAgeOld(age) {
- if (age < 30) {
- return "You’re a youngster!";
- } else {
- return "You guys like MySpace?!";
- }
- }
- var age = 27;
- console.log(isMyAgeOld(age));
- /* Answer:
- You’re a youngster!
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement