Functions pt1

PHOTO EMBED

Tue Jun 21 2022 21:22:16 GMT+0000 (Coordinated Universal Time)

Saved by @cruz #javascript

let names = ["Lena", "James", "Julio"];

for (let i = 0; i < names.length; i++) {
   console.log(names[i]);
}
//Following this pattern, we can create a function that prints any array of names.
function printNames(names) {
   for (let i = 0; i < names.length; i++) {
      console.log(names[i]);
   }
}
//Notice that there is nothing about this function that forces names to actually contain names, or even strings. The function will work the same for any array it is given. Therefore, a better name for this function would be printArray.Our function can be used the same way as each of the built-in functions, such as console.log, by calling it. Remember that calling a function triggers its actions to be carried out.

function printArray(names) {
   for (let i = 0; i < names.length; i++) {
      console.log(names[i]);
   }
}

printArray(["Lena", "James", "Julio"]);
console.log("---");
printArray(["orange", "apple", "pear"]);

function sayHello() {
   console.log("Hello, World!");
}
sayHello();

function sayThanks(name) {
  console.log('Thank you for your purchase '+ name + '! We appreciate your business.');
}

sayThanks("Cole");

///example of functions with default values

function makeShoppingList(item1 = 'milk', item2 = 'bread', item3 = 'eggs'){
  console.log(`Remember to buy ${item1}`);
  console.log(`Remember to buy ${item2}`);
  console.log(`Remember to buy ${item3}`);
}

content_copyCOPY