GithubHelp home page GithubHelp logo

2019-spring-08-javascript-functions-returns's Introduction

CoderBabez

Week Eight - Variables, Functions & Returns

Objectives

Understand the purpose of "return", and use it within a function.

Vocab

  • Variable
  • Functions
  • Parameters
  • Returns

Review

What is the result of the following pieces of code?

Question #1:

function mystery(red) {
  $("body").css("background","red")
}
mystery("blue");

Question #2:

function mystery() {
  $("body").css("background","red")
}

Question #3:

var c = "green"
function mystery(x) {
  $("body").css("background",c)
}
mystery("blue");

Question #4:

var c = "green"
function mystery(x) {
  $("body").css("background",x)
}
mystery(c);

Lesson

A function is a reusable set of instructions

A function can have....

  • inputs (parameters)
  • outputs (return)

Let's look at an example:

function secret(x,y) {
  var result = x*y;
  return result;
}

var y = secret(3,2);

Check for understanding:

  • What do you think will be stored in y when this code runs?
  • What is the parameter, the argument and the return value in this piece of code?

Jquery also has some important functions that return data. Today we're going to learn about .val()! .val() let's us get the text that a user has written into a text field on our webpage - like an email, name or comment field.

Let's walk through an example....

<input id="comment"></input>
<button>Submit</button>
$(document).ready(function(){
  $("button").click(function(){
    var userComment = $("#comment").val();
  });
});

Check for understanding:

  • What do you think this code is doing?
  • What is stored in userComment when the page loads?
  • What is stored in userComment when the button is clicked?

Practice

Practice your functions and returns! In the HTML file, create a name input field, and a submit button.

In the Javascript file,

  • Write a function called getName that gets the current value of the name input field, and returns it.
  • Create an event handler that calls getName when the button is clicked, stores the return value in a variable, and then appends "Hello {{name}}" to the body of the html page.
  • BONUS: create a second function called greeting, that takes a name parameter, and appends "Hello {{name}}" when called. Update your code above to use your new greeting function

Homework

Complete the calculator project in the homework folder. Instructions are in the javascript file.

Reference:

2019-spring-08-javascript-functions-returns's People

Contributors

megknoll avatar

Watchers

 avatar  avatar  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.