Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

License

NotificationsYou must be signed in to change notification settings

gititmel/javascript-intro-to-functions-lab-js-intro-000

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Objectives

  1. Practice writing functions
  2. Explain basics of working with strings
  3. Explain the difference betweenreturn and logging
  4. Practice usingreturn andconsole.log()

Introduction

Welcome to the JavaScript functions lab! You'll notice a few new things in this lesson that we haven't encountered before. Don't worry, we'll walk you through them.

Even if you've walked through some of this material before, it's a good idea to review as we code-along — we're writing functions now, after all.

Code-along

For now, open upindex.js in your text editor. You should see, well, nothing. We'll fix that soon.

Now open uptest/root.js. Hey, there's something! What's all of this stuff doing?

At the very top of the file, you'll see

global.expect=require('expect');constbabel=require('babel-core');constjsdom=require('jsdom');constpath=require('path');

This might be a bit bewildering, but all we're doing is referencing differentlibraries that help us run your tests. A library is code that someone else (usually multiple someone elses) wrote for our use. Note thatrequire won't work out of the box in the browser. We're actually running our tests in a differentenvironment. (Remember the sandbox analogy from earlier? It's just like that.)

If you go totest/index-test.js, you'll see

describe('shout(string)',()=>{// there's stuff in here, too})

describe is a function provided by our test runner (in this case, we're usingMocha) — it's basically a container for our tests.

Let's take a closer look at thatdescribe():

describe('shout(string)',()=>{it('receives one argument and returns it in all caps',()=>{// we'll get to this in a sec})})

These internaldescribe() calls are used fordescribing the functions that you're going to write. In this case, the test is saying, "Okay, I think there's going to be a function calledshout, and it should take one argument (it doesn't actually matter what the argument is called, butstring, is nice and specific, don't you think?). It shouldreturn that argument in all caps.

Finally, we have

expect(shout('hello')).toEqual('HELLO')

which says that itexpects a call toshout() with the string'hello' willequal the string'HELLO'. This is the actual test — otherwise called a spec, expectation, or assertion — for this function. We can have more than one test per function, but let's start with this one.

Running the Tests

To run the tests, runlearn test in the terminal in your Learn IDE. The first output you'll see will look like

test fail

Hm, that's kind of disappointing. Let's see if we can get that first test to pass. Open upindex.js.

When we write our code, we follow the guidance of the tests. Remember the line,describe('shout(string)', () => { ... }). Well, we know that we need a function calledshout that accepts an argument — let's add that first. Inindex.js:

functionshout(string){}

And what should that function do? Well, theit() description tells us that it "receives one argument and returns it in all caps".

Okay, so with that information, we know that our function should look like

functionshout(string){returnstring}

But how do we makestring all caps? JavaScript has a method for that! It's calledtoUpperCase(). We can call it on any string:

'Hello!'.toUpperCase()// 'HELLO!'

So let's try it with ourshout() function:

functionshout(string){returnstring.toUpperCase()}

And run our tests again:

learn

first test passing

Hey! We got one to pass!

Your turn

Now it's your turn to get the rest of the tests to pass. Note that some of them require you to useconsole.log() instead ofreturn — follow the guidance of the tests!

In this lab, we're writing functions that "speak" at different volumes — they whisper or they shout. We're going to use what we learn practicing speaking in this way to write another function,sayHiToGrandma(), which takes our new-found speaking ability to greet our grandmother. She's not exactly deaf, but whispering can be a bit difficult. But she'll always hear you if you say, "I love you, Grandma."

Note that just like.toUpperCase() changes any string to all uppercase in JavaScript,.toLowerCase() (e.g.,'HELLO'.toLowerCase()) changes any string to all lowercase.

Additionally, how do we check if a string is all lowercase or all uppercase?

varuppercase="HELLO!"uppercase.toUpperCase()===uppercase// truevarlowercase='hello!'lowercase.toLowerCase()===lowercase// truevarmixedCase='Hi there!'mixedCase.toLowerCase()===mixedCase// falsemixedCase.toUpperCase()===mixedCase// false

We can simply check whether the string is the same when we convert it to uppercase or lowercase! If it's the same, then it was already in that case; if not, then it's either in the other case or it's mixed case.

Good luck! When you're finished, be sure to runlearn submit!

ViewIntro to Functions Lab on Learn.co and start learning to code for free.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript76.6%
  • HTML23.4%

[8]ページ先頭

©2009-2025 Movatter.jp