- Notifications
You must be signed in to change notification settings - Fork0
License
dancoder-engineer/javascript-intro-to-functions-lab-js-intro-000
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
- Practice writing functions
- Explain basics of working with strings
- Explain the difference between
return
and logging - Practice using
return
andconsole.log()
Welcome to the JavaScript functions lab! You'll notice a few new things in thislesson that we haven't encountered before. Don't worry, we'll walk you throughthem.
Even if you've walked through some of this material before, it's a good idea toreview as we code-along — we're writing functions now, after all.
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 stuffdoing?
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'twork out of the box in the browser. We're actually running our tests in adifferentenvironment.
If you go totest/index-test.js
, you'll see
describe('shout(string)',function(){// 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)',function(){it('receives one argument and returns it in all caps',function(){// we'll get to this in a sec})})
These internaldescribe()
calls are used fordescribing the functions thatyou're going to write. In this case, the test is saying, "Okay, I think there'sgoing to be a function calledshout
, and it should take one argument (itdoesn't actually matter what the argument is called, butstring
, is nice andspecific, 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 testper function, but let's start with this one.
To run the tests, runlearn test
in the terminal in your Learn IDE. The firstoutput you'll see will look like
> java-script-intro-to-functions-lab@0.1.0 test /Users/mbenton/Desktop/curriculum-team/junk/javascript-intro-to-functions-lab> mocha -R mocha-multi --reporter-options spec=-,json=.results.json --timeout 10000 shout(string) 1) receives one argument and returns it in all caps whisper(string) 2) receives one argument and returns it in all lowercase logShout(string) 3) calls console.log() its one argument in all caps logWhisper(string) 4) calls console.log() its one argument in all lowercase sayHiToGrandma(string) 5) returns "I can't hear you!" if `string` is lowercase 6) returns "YES INDEED!" if `string` is uppercase 7) returns "I love you, too." if `string` is "I love you, Grandma."` 0 passing (99ms) 7 failing 1) shout(string) receives one argument and returns it in all caps: ReferenceError: shout is not defined at Context.<anonymous> (test/index-test.js:4:5) at processImmediate (internal/timers.js:456:21) 2) whisper(string) receives one argument and returns it in all lowercase: ReferenceError: whisper is not defined at Context.<anonymous> (test/index-test.js:10:5) at processImmediate (internal/timers.js:456:21) 3) logShout(string) calls console.log() its one argument in all caps: ReferenceError: logShout is not defined at Context.<anonymous> (test/index-test.js:18:5) at processImmediate (internal/timers.js:456:21) 4) logWhisper(string) calls console.log() its one argument in all lowercase: ReferenceError: logWhisper is not defined at Context.<anonymous> (test/index-test.js:30:5) at processImmediate (internal/timers.js:456:21) 5) sayHiToGrandma(string) returns "I can't hear you!" if `string` is lowercase: ReferenceError: sayHiToGrandma is not defined at Context.<anonymous> (test/index-test.js:40:5) at processImmediate (internal/timers.js:456:21) 6) sayHiToGrandma(string) returns "YES INDEED!" if `string` is uppercase: ReferenceError: sayHiToGrandma is not defined at Context.<anonymous> (test/index-test.js:44:5) at processImmediate (internal/timers.js:456:21) 7) sayHiToGrandma(string) returns "I love you, too." if `string` is "I love you, Grandma."`: ReferenceError: sayHiToGrandma is not defined at Context.<anonymous> (test/index-test.js:48:5) at processImmediate (internal/timers.js:456:21)npm ERR! Test failed. See above for more details.
Hm, seven failed tests. Let's see if we can get thatfirst test topass. Open upindex.js
.
When we write our code, we follow the guidance of the tests. Remember the line,describe('shout(string)', function() { ... })
. Well, we know that we need afunction 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 this:
functionshout(string){returnstring}
But how do we makestring
all caps? JavaScript has a method for that! It'scalledtoUpperCase()
. 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:
learntest
shout(string) ✓ receives one argument and returns it in all caps whisper(string) 1) receives one argument and returns it in all lowercase logShout(string) 2) calls console.log() its one argument in all caps logWhisper(string) 3) calls console.log() its one argument in all lowercase sayHiToGrandma(string) 4) returns "I can't hear you!" if `string` is lowercase 5) returns "YES INDEED!" if `string` is uppercase 6) returns "I love you, too." if `string` is "I love you, Grandma."` 1 passing (108ms) 6 failing 1) whisper(string) receives one argument and returns it in all lowercase: ReferenceError: whisper is not defined at Context.<anonymous> (test/index-test.js:10:5) at processImmediate (internal/timers.js:456:21) 2) logShout(string) calls console.log() its one argument in all caps: ReferenceError: logShout is not defined at Context.<anonymous> (test/index-test.js:18:5) at processImmediate (internal/timers.js:456:21) 3) logWhisper(string) calls console.log() its one argument in all lowercase: ReferenceError: logWhisper is not defined at Context.<anonymous> (test/index-test.js:30:5) at processImmediate (internal/timers.js:456:21) 4) sayHiToGrandma(string) returns "I can't hear you!" if `string` is lowercase: ReferenceError: sayHiToGrandma is not defined at Context.<anonymous> (test/index-test.js:40:5) at processImmediate (internal/timers.js:456:21) 5) sayHiToGrandma(string) returns "YES INDEED!" if `string` is uppercase: ReferenceError: sayHiToGrandma is not defined at Context.<anonymous> (test/index-test.js:44:5) at processImmediate (internal/timers.js:456:21) 6) sayHiToGrandma(string) returns "I love you, too." if `string` is "I love you, Grandma."`: ReferenceError: sayHiToGrandma is not defined at Context.<anonymous> (test/index-test.js:48:5) at processImmediate (internal/timers.js:456:21)npm ERR! Test failed. See above for more details.
Hey! We got one to pass! Six left.
Now it's your turn to get the rest of the tests to pass. Note that some of themrequire you to useconsole.log()
instead ofreturn
— follow the guidance ofthe tests!
In this lab, we're writing functions that "speak" at different volumes — theywhisper or they shout. The next test is similar to the first:
1) whisper(string) receives one argument and returns it in all lowercase: ReferenceError: whisper is not defined at Context.<anonymous> (test/index-test.js:10:5) at processImmediate (internal/timers.js:456:21)
This test is telling us thatwhisper(string)
receives one argument and returnsit in all lowercase. At the moment, the test is failing becasue whisper is notdefined.
Note: Just like
.toUpperCase()
changes any string to all uppercase inJavaScript,.toLowerCase()
(e.g.,'HELLO'.toLowerCase()
) changes anystring to all lowercase.
The next two tests are checking to see if a specific string is logged when afunction is called. You will still need to use the.toUpperCase()
and.toLowerCase()
methods forlogShout(string)
andlogWhisper(string)
. Keep inmind though that these tests are not looking for return values, only logs.
The final function you need to create issayHiToGrandma()
. Grandma is a bithard of hearing, so whispering can be a bit difficult, but she'll always hearyou if you say, "I love you, Grandma." This time, you will need to returndifferent strings depending on the string passed into the function.
Note: Although there are 3 tests for
sayHiToGrandma()
, you only need towriteone function. This function should be able to handle all three testconditions:
- If the string that is passed into the function is all lowercase, thefunction should return "I can't hear you!"
- If the string that is passed into the function is all uppercase, thefunction should return "YES INDEED!"
- If the string that is passed into the function is equal to "I love you,Grandma.", the function should return "I love you, too."
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 touppercase or lowercase! (The lines with the===
comparisons are the ones thatcheck). If it's the same, then it was already in that case; if not, then it'seither in the other case or it's mixed case. Now that we know how to comparestrings, how can we use these comparisons to conditionally return differentstrings?
Remember that punctuation is important! Humans might be able to understand that"I love you Grandma" is close enough to "I love you, Grandma." and means thesame thing but JavaScript will not consider these equal!
Good luck! When all tests are passing, be sure to runlearn submit
!
ViewIntro to Functions Lab on Learn.co and start learning to code for free.
About
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Languages
- JavaScript77.0%
- HTML23.0%