GithubHelp home page GithubHelp logo

js-basics-functions-lab-dc-fe-071018's Introduction

Functions Lab

Learning Goals

  • Practice writing functions
  • Explain calling functions from within other functions
  • Practice basics of control flow and return statements

Introduction

Scuber, our transportation company, has gained some traction among moms and dads in New York City. It has also received a few requests.

First, Scuber's executives want to ensure that Scuber's headquarters is near its customers. They would like you to write a function that takes in a pickup location for a passenger, and returns the number of blocks from its headquarters on 42nd Street. For example, if the pickup location is 50 (i.e., 50th Street), the return value should be 8. The function's return value can then be used for another function that translates the number of blocks from headquarters to the distance in feet.

Second, customers want Scuber to calculate the number of feet travelled based on the distance. Write a function called distanceTravelledInFeet that takes in the beginning and destination blocks and returns the number of feet travelled. Each block in Manhattan is 264 feet long. For example distanceTravelledInFeet(34, 38) representing 34th St to 38th St, returns 1056 ((38-34)*264). You can assume that we are only calculating distance uptown/downtown, not from river to river.

Instructions

Run learn and look at the tests currently breaking. You will need to write these functions from scratch, and test them as you go to get them working. Remember to look through the tests, which are located in test/indexTest.js. It's a bit easier to think through the testing process in steps.

  1. The first describe statement, describe('index.js', function(), tells us the file being tested. Here, it's the index.js file.
  2. Our second describe statement, describe('distanceFromHqInBlocks()', function(), lets us know what the name of our function is. In this case, our test is expecting to find a function called distanceFromHqInBlocks().
  3. Our third statement, it('returns a distance in blocks', function(), describes what our function is doing. In this case, we're going to be calculating some kind of distance.
  4. Finally, our last statement, expect(distanceFromHqInBlocks(43)).to.equal(1), tells us what our test is expecting the result to be. There are a couple really important clues here. Our first one is the 43. That tells us that our function is expecting an argument to be passed in, so we'll need to specify some kind of variable. Our other clue is the 1, because that tells us what the result is. If we're passing in 43, we're expecting our function to calculate that it's 1 block from headquarters.

Try reading through all of the tests first, before you write any code! It will help you decide how to structure your code in the cleanest and most efficient manner.

To complete all of your work assignments, you'll need to write four functions:

  • distanceFromHqInBlocks: Returns the number of blocks from Scuber's headquarters to the pickup location.

  • distanceFromHqInFeet: Returns the number of feet from Scuber's headquarters to the pickup location. You can use your distanceFromHqInBlocks function to help return the correct value here. Try something like this:

function distanceFromHqInBlocks (someValue) {
  //returns the number of blocks given a value
}

function distanceFromHqInFeet (someValue) {
  distanceFromHqInBlocks(someValue);
  // calls distanceFromHqInBlocks from inside the distanceFromHqInFeet function, passing the argument from distanceFromHqInFeet into distanceFromHqInBlocks
  //the return value of distanceFromHqInBlocks can then be used to calculate feet
}
  • distanceTravelledInFeet: Calculates the number of feet a passenger travels given a starting block and an ending block โ€” it only calculates distance North and South (uptown/downtown). It uses the knowledge that a block is 264 feet long.

  • calculatesFarePrice: Given the same starting and ending block as the previous test (hint hint), return the fare for the customer. The first four hundred feet are free. For a distance between 400 and 2000 feet, the price is 2 cents per foot (not including 400, which are free!). Then Scuber charges a flat fare for a distance over 2000 feet and under 2500 feet. Finally, Scuber does not allow any rides over 2500 feet โ€” the function returns 'cannot travel that far' if a ride over 2500 feet is requested.

js-basics-functions-lab-dc-fe-071018's People

Contributors

gj avatar jeffkatzy avatar lizbur10 avatar alexgriff avatar maxwellbenton avatar cjbrock avatar aviflombaum avatar joannasese avatar drakeltheryuujin avatar sgharms avatar

Watchers

 avatar Rishikesh Tirumala avatar James Cloos avatar  avatar Victoria Thevenot avatar  avatar Joe Cardarelli avatar Sam Birk avatar Sara Tibbetts avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Antoin avatar  avatar  avatar Amanda D'Avria avatar  avatar A. Perez avatar Nicole Kroese  avatar Lore Dirick avatar Nicolas Marcora avatar Maria Kennedy avatar Lisa Jiang 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.