Giter Site home page Giter Site logo

coderbabez-wk7-js-3's Introduction

CoderBabez

Week Seven - Variables & Functions

Objectives

Use variables and functions to add and remove puppy pictures on an html page.

Vocab

  • Variable
  • Functions
  • Parameters

Review

A function is a reusable set of instructions

Functions are defined. The function definition writes out all the things the function will do.

function functionName() {
  // do stuff
}

Functions do stuff only when they are called. Call a function by its name with parentheses after.

functionName();

Some functions make something change

function changeBackground() {
  $('body').css('background', 'red');
}

Other functions return a value

function getColor() {
  return 'red';
}

Functions can be used together!

function changeBackground() {
  $('body').css('background', getColor());
}

Functions can take a parameter. A parameter is additional information given to the function that it can use to do something. You pass a parameter in the parentheses when you call a function.

function changeBackground(colorName) {
  $('body').css('background', colorName);
}
changeBackground('red')

Variables

Variables store data. They have a name you use to refer to the data.

We use the word var to declare the variable. We use the = to define the value of the variable. Defining a value with the = is also called assigning a value.

var x = 1;
var y = 2;
var z = 1 + 2;

What are the variables above? What are their values?

You can change the data that's in a variable

var x = 1;
x = 2;

What is the value of x in each line? How many times did we declare x? How many times did we assign a value to x?

Variables can store lots of different types of data. Strings and numberse are common ones we'll start with.

var a = "I am a string!";
var b = 'I am also a string';
var c = 1;
var d = 1.3456;

What types of values have been assigned to these variables?

You can pass variables to functions as parameters

function changeBackground(colorName) {
  $('body').css('background', colorName);
}
var color = 'red';
changeBackground(color);

Project

Last week we used functions to update multiple things on a page at once. This week we're going to add a variable that keeps track of the state of the page.

Take a look at the live site:

  • What are the html elements?
  • What changes when the button is clicked?
  • What stays the same between clicks?
  • How would you go about implementing this page?

Reference:

coderbabez-wk7-js-3's People

Contributors

becsegal avatar tazzai avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.