Giter VIP home page Giter VIP logo

js-functions-lab's Introduction

JavaScript Functions

Objectives

  1. Write a function that returns a value
  2. Write a function that takes in a parameter
  3. Write a function that takes in multiple parameters

Introduction

You'll be writing your solution in index.js.

In this lab, we're going to develop our communication skills in JavaScript. We're feeling festive, so we're going to be wrapping up common holiday greetings as functions so that we don't have to repeat ourselves. The beauty of functions is that we could reuse these functions for the text of greeting cards, for spoken greetings, for song lyrics, etc...

Template Literals

There are two ways main ways to include variables inside a string. Say we had a variable named date that we assign to a value:

var date = "July 3rd"

In JavaScript, we can use operators to concatenate (join) two strings, or in this case, a string and a variable, like so:

console.log("My birthday is " + date)

With date defined, the above code will log My birthday is July 3rd. However, by using a slightly modified syntax, we can achieve the same thing by embedding a variable into a string. These are called template literals and rewriting the above console.log with one would look like this:

console.log(`My birthday is ${date}`)

This will also log My birthday is July 3rd.

Now, there are two important changes to the syntax: Any variables we want to include must be wrapped in curly braces ({ }) and preceded by a dollar sign ($). In addition, instead of single, ', or double quotes, ", we must use backticks, `. If backticks are not used, JavaScript will interpret the dollar sign and curly braces as a normal part of the string, resulting in My birthday is ${date}! Any expression can be included in template literals, not just variables, so we could write something like:

console.log(`I have ${1 + 1} pets`)

And get I have 2 pets. Note that this will not log the same thing if you did the following:

console.log("I have " + 1 + 1 + " pets")

This logs I have 11 pets! JavaScript converts both 1s into strings rather than adding them together first. You would need to write the following to get the same result as template literals:

console.log("I have " + (1 + 1) + " pets")

You can use either operators or template literals to construct larger strings with dynamic values. Template literals are just a way to make your code a little easier to read and to help ensure JavaScript does not misinterpret when combining different data types into strings, like we just saw.

Instructions

  1. Write a function named happyHolidays. This function should not accept any parameters and should return the string "Happy holidays!".

  2. Write a function named happyHolidaysTo. This function should accept a parameter of the name of the person you want to wish happy holidays, and return the string `Happy holidays, ${name}!`

  3. Write a function named happyCustomHolidayTo. This function should accept two parameters, the holiday you want to wish them well for, and the name of the person you're wishing well. Order of parameters matters, so make sure to first pass in the holiday and then the name. This function should return the string `Happy ${holiday}, ${name}!`

  4. Write a function named holidayCountdown. This function should accept two parameters, the holiday name and number of days until that holiday. The function should return the string `It's ${days} days until ${holiday}!`. Note that although days is used first when constructing the returned string, holidayCountdown should take in the holiday name first, then the days until that holiday.

js-functions-lab's People

Contributors

annjohn avatar bhollan avatar changfenxia avatar danielseehausen avatar dependabot[bot] avatar franknowinski avatar gj avatar ihollander avatar ipc103 avatar lizbur10 avatar maxwellbenton avatar pletcher avatar rrcobb avatar victhevenot avatar wolfhoward avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-functions-lab's Issues

How to make functions

hey im making functions in learn IDE and here's the problem. I can create a function like :

function happyHolidays
console.log("Happy Holidays!!")
i am positive i am doing everything correct and have even copy pasted some of the provided codes and they all give me the same error.

When i type into the console or where i enter the code: function HappyHolidays i always get this weird thing in the console where instead of a reply i just get this weird arrow that pops up, like a directory or something its strange, http://prntscr.com/gwqpsm

and i never can even get a response to any of my functions :(

Functions & Scope / Functions Lab / Not loading IDE

Hi,

Loaded the Functions Lab in Function & Scope/Javascript Fundamentals, however doesn't seem to be loading the IDE files (ie index.js). The previous lab worked, not sure what I've done different. Lab says it's been forked, but none of the analysis stuff is occurring in the bottom black box in IDE.

any help would be greatly appreciated

Functions not working...

I wrote the functions as stated in the lesson. When I type learn test in LEARN IDE, I get the following errors:

1) functions happyHolidaysTo(name) returns "Happy holidays, ${name}!":                                                                                                                                                       
                                                                                                                                                                                                                               
      Error: Expected 'Happy holidays, ${name}!' to equal 'Happy holidays, you!'                                                                                                                                               
      + expected - actual                                                                                                                                                                                                      
                                                                                                                                                                                                                               
      -Happy holidays, ${name}!                                                                                                                                                                                                
      +Happy holidays, you!                                                                                                                                                                                                    
                                                                                                                                                                                                                               
      at assert (node_modules/expect/lib/assert.js:29:9)                                                                                                                                                                       
      at Expectation.toEqual (node_modules/expect/lib/Expectation.js:81:30)                                                                                                                                                    
      at Context.it (test/index-test.js:12:38)                                                                                                                                                                                 
                                                                                                                                                                                                                               
  2) functions happyHolidayTo(holiday, name) returns "Happy ${holiday}, ${name}!":                                                                                                                                             
                                                                                                                                                                                                                               
      Error: Expected 'Happy ${holiday}, ${name}!' to equal 'Happy Independence Day, you!'                                                                                                                                     
      + expected - actual                                                                                                                                                                                                      
                                                                                                                                                                                                                               
      -Happy ${holiday}, ${name}!                                                                                                                                                                                              
      +Happy Independence Day, you!                                                                                                                                                                                            
                                                                                                                                                                                                                               
      at assert (node_modules/expect/lib/assert.js:29:9)                                                                                                                                                                       
      at Expectation.toEqual (node_modules/expect/lib/Expectation.js:81:30)                                                                                                                                                    
      at Context.it (test/index-test.js:18:57)                                                                                                                                                                                 
                                                                                                                                                                                                                               
  3) functions holidayCountdown(holiday, days) returns "It's ${days} until ${holiday}!":                                                                                                                                       
                                                                                                                                                                                                                               
      Error: Expected 'It\'s ${days} until ${holiday}!' to equal 'It\'s 20 days until Mother\'s Day!'                                                                                                                          
      + expected - actual                                                                                                                                                                                                      
                                                                                                                                                                                                                               
      -It's ${days} until ${holiday}!                                                                                                                                                                                          
      +It's 20 days until Mother's Day!                                                                                                                                                                                        
                                                                                                                                                                                                                               
      at assert (node_modules/expect/lib/assert.js:29:9)                                                                                                                                                                       
      at Expectation.toEqual (node_modules/expect/lib/Expectation.js:81:30)                                                                                                                                                    
      at Context.it (test/index-test.js:24:52)

I don't think the functions I've written are incorrect.

NOT RUNNING

Canvas Link

Testing with javascript lab

Concern

[email protected] test
mocha --timeout 5000 -R mocha-multi --reporter-options spec=-,json=.results.json

  1. "before all" hook in "{root}"

0 passing (375ms)
1 failing

  1. "before all" hook in "{root}":
    TypeError: require(...).env is not a function
    at Context. (node_modules/mocha-jsdom/index.js:52:22)
    at processImmediate (node:internal/timers:466:21)

Additional Context

No response

Suggested Changes

No response

Debugger not working

screen shot 2016-07-21 at 8 35 06 pm

The above errors are displayed in my console when I run "npm run debug" in my terminal. I ran npm install before running "npm run debug", and had the debugger keyword in my second function as follows:

function sayHey() {
  return "Hey!";
}

function sayHeyFriend(name) {
  debugger;
}

When I hovered over "name" in Chrome, "Kristen" did not come up. Nor did it come up when I typed it into the console as outlined in the lab.

screen shot 2016-07-21 at 8 39 58 pm

Why is my code failing the local tests?

function happyHolidays() {
return "Happy holidays!";
}

function happyHolidaysTo(name) {
return "Happy holidays, ${name}!";
}

function happyHolidayTo(holiday, name) {
return 'Happy ${holiday}, ${name}!';
}

function holidayCountdown(holiday, days) {
return "It's ${days} days, until ${holiday}!";
}

Task 4 Test - Potential Confusion For Students

Test
describe('holidayCountdown(holiday, days)', () => {
it('returns "It's ${days} days until ${holiday}!"', () => {
expect(holidayCountdown("Mother's Day", 20)).toEqual("It's 20 days until Mother's Day!")

JS User Input
function holidayCountdown(holiday, days) {
return(It\'s ${days} days until ${holiday}!)

Comment
(holiday, days) parameters being flipped is a bit unintuitive when comparing with the return sentence. May want to consider flipping the two parameters to mitigate student confusion. That's assuming this was not intentional!

Functions Lab

// write your code below! The first 3 is correct only the last one I cannot sort out
function happyHolidays(string) {
return "Happy holidays!"

}

function happyHolidaysTo(name) {
return ('Happy holidays, you!')

}

function happyHolidayTo(holiday, name){
return('Happy Independence Day, you!')
}

function holidayCountdown(holiday, days){
return ("It's ${days} days until ${holiday}!")
return ("Mother's day",(${days}=20))

}

Functions Lab

function happyHolidays(null) {
return ("Happy holidays!")
}

function happyHolidaysTo(name) {
return ("Happy holidays, ${name}!")
}

  1. functions happyHolidays returns "Happy holidays!":
    ReferenceError: happyHolidays is not defined
    at Context.it (test/index-test.js:6:14)

  2. functions happyHolidaysTo(name) returns "Happy holidays, ${name}!":
    ReferenceError: happyHolidaysTo is not defined
    at Context.it (test/index-test.js:12:14)

  3. functions happyHolidayTo(holiday, name) returns "Happy ${holiday}, ${name}!":
    ReferenceError: happyHolidayTo is not defined
    at Context.it (test/index-test.js:18:14)

  4. functions holidayCountdown(holiday, days) returns "It's ${days} until ${holiday}!":
    ReferenceError: holidayCountdown is not defined
    at Context.it (test/index-test.js:24:14)

am I missing something to make sure that the functions get defined?

Backtick Information Missing

I would like to bring attention to the lack of information in the Functions Lab about the necessity of backticks when using template literals like ${name}. It took me a good 20 minutes of trial & error and googling before I discovered the whole issue was that I was using normal apostrophes ( ' ) instead of backticks ( ` ). I don't think there was any mention of this, but if it was mentioned, I missed it, and perhaps it should be repeated! I'm just trying to help out my fellow students and I hope this can improve the lessons!

IDE Error

My IDE will not open, even if I"m using Chrome and it's updated, even after refreshing several times.

js functions lab error

Hello,

I am receiving an error on the final function on the "js-functions-lab:

  1. functions holidayCountdown(holiday, days) returns "It's ${days} until ${holiday}!":
    ReferenceError: holidayCountdown is not defined
    at Context.it (test/index-test.js:24:14)

Below is what I have written to receive that error:

function happyHolidayCountdown(holiday, days) {
return It\'s ${days} days until ${holiday}!
}

The other functions written in the same manner are passing the test. Any help would be greatly appreciated. Thank you.

Missing instructions on running local tests

I believe the order of labs is off here if tests are required on this one. I followed instructions from the next lesson . When I type "Learn" in my terminal I get "This directory doesn't appear to have any specs in it." I'll raise an issue in that one if it persists.

I installed mocha and added in the missing node dependencies that are in the head of the index.html file and was able to view tests in my browser.
`<script src="node_modules/mocha/mocha.js"></script>

<script src="node_modules/expect/umd/expect.min.js"></script>`

I don't think installation was covered in the material so far or it should be added to the readme for this lab.

Then I submitted a pull request even though I can't complete the local test step.

tests pass but Learn app shows local test not run

I had to get help to run tests, which succeeded, using the following command: npm run-script test

// โ™ฅ npm run-script test

[email protected] test /Users/BradSmith/Development/code/js-functions-lab-v-000
mocha -R mocha-multi --reporter-options nyan=-,json=.results.json

4 ---,------,
0 -
--| /_/\
0 ---^|__( ^ .^)
--- "" ""

4 passing (494ms)

Running a function in Learn IDE

Quick question - if I was to write a function in a file named index.js:

function(name) {
return "My name is ${name}."
}

What steps would I need to take to test this function? As of now, I am saving the file and running "nodejs index.js" in terminal. My goal is to enter a name after running the function and have the output be "My name is ____".

Thank you.

Issue with this project

I believe there is something wrong with the learn IDE. I submitted this and its telling me I am wrong when I believe that I am correct. I verified in QA section and someone with the identical code passed. Please advise.

function happyHolidays() {
return Happy holidays!
}

function happyHolidaysTo(name) {
return Happy holidays, ${name}!
}

function happyHolidaysTo(holiday name) {
return Happy ${holiday}, ${name}!
}

function holidayCountdown(days holiday) {
return It's ${days} days until ${holiday}!
}

failed

this is not specific to this lab bur by every lab if you fail it says falled in stead of failed by the side where its tells your your progress and if you've forked the lab or not

Issue w/Functions Lab

The second function description says:
Write a function named happyHolidaysTo. This function should accept a parameter of the name of the person you want to wish happy holidays, and return the string Happy holidays, ${name}!

  1. functions happyHolidaysTo(name) returns "Happy holidays, ${name}!":

    Error: Expected 'Happy holidays, ${name}!' to equal 'Happy holidays, you!'

    • expected - actual

    -Happy holidays, ${name}!
    +Happy holidays, you! //No mention of 'Happy holidays, you!' anywhere in this lab, but that's the correct answer to get the test to pass. Is this a mistake, or am I just a noob and missing something?

    at assert (node_modules/expect/lib/assert.js:29:9)
    at Expectation.toEqual (node_modules/expect/lib/Expectation.js:81:30)
    at Context.it (test/index-test.js:12:38)

IDE App doesn't want to start

Hi,
Every time I try to launch the IDE app, have to find the install.exe file, and reinstall it, otherwise it won't start. Is there any solution there?
Many Thanks,
Alpar

Test case misnamed.

functions holidayCountdown(holiday, days) returns "It's ${days} until ${holiday}!":

Should be:

functions holidayCountdown(holiday, days) returns "It's ${days} days until ${holiday}!":

Refactor for skills-based track

  • Remove extra link
  • [ ] Make the lab harder -- it's at the point in the curriculum where students can struggle a little bit and that'll be okay
  • Refactor the tests so they don't require a browser

Question 4 Parameter word order.

The question asks for a return string of return It\'s ${days} days until ${holiday}!

To pass the test the parameter needs to have (days,holiday) in it but has to be backwards to the string meaning it has to be (holiday,days). In a real life code the order wouldn't matter but for the test it does.
Very confusing for new students learning functions and it's parameters.

Learn IDE

I can't seem to see the bottom of the terminal pane after a few processes take place. Does anyone have a fix for it?
image

When I type "learn" in the command line, it says the test directory is empty

For the js-functions lab, when I am in my local directory and type "learn" into the command line, it says, "This directory doesn't appear to have any specs in it." I can run the tests by doing npm test and see that they are all passing, but I can't get the "Run local tests" button to turn green on my learn page to move on to the next lesson.

Functions Lab

Hello. I'm failing Functions Lab. My error message says that I've not defined the function names in index-test.js. Can't figure out what I'm doing wrong. The questions seem so cut and dry.

Suggestion - Funcitons lab

Spent an unnecessary amount of time here because I didn't know that you needed to use backticks ( ` ), and not single or double quotes to exclude variables from being literals in the string.

Maybe a one or 2 sentence call out about backticks would save other some time as well? - food for thought.

JS Functions Lab

I seem to be having the same issue as others with this functions lab. I realized after the first 2 that something was off because my error came back with:

  1. functions happyHolidays returns "Happy holidays!":
    ReferenceError: happyHolidays is not defined
    at Context.it (test/index-test.js:6:7)
  2. functions happyHolidaysTo(name) returns "Happy holidays, ${name}!":
    ReferenceError: happyHolidaysTo is not defined
    at Context.it (test/index-test.js:12:7)
  3. functions happyHolidayTo(holiday, name) returns "Happy ${holiday}, ${name}!":
    ReferenceError: happyHolidayTo is not defined

my code:

function happyHolidaysTo(string){
var name = "josh";
return Happy holidays, ${name}!
}

function happyHolidayTo(holiday, name){
// create var for parameters
var holiday = "easter";
var name = "crista";
// return value
return Happy ${holiday}, ${name}!;
}

// write a function
function holidayCountdown(holiday, days){
// create variables
var holiday = "Thanksgiving";
var days = "9";
// return value
return It's ${days} days until ${holiday}!;

ANY HELP WOULD BE GREAT!

Has 3 lights instead of 2 -- no tests?, file structure conflict

This lab has 3 lights, in talking with an learn expert it is supposed to have 2. Right now it requests local tests in addition to a fork and pull request. Because it doesn't have any specs, you can't get all 3 greens and officially "pass" the lesson.

Also, it asks you to code in js/function.js but there is not a js directory nor a solution.js file. In the actual package it initially asks you to write in index.js which is different than the current README.

js-functions-lab

Not sure why only 1 of 4 tests is passing in the Functions lab.

After trying a few different things, current failing code in the index.js is:

function happyHolidaysTo(name) {
return 'Happy holidays, ${name}'
}
happyHolidaysTo(you)

function happyHolidayTo(holiday, name) {
return "Happy ${holiday}, ${name}"
}

function holidayCountdown(days, holiday) {
return "It's ${days} days until ${holiday}!"
}

Can anyone assist?
Thanks very much,

Bobby

Js-functions (holidayCountdown) I need some help

// write your code below! The first 3 is correct only the last one I cannot sort out
function happyHolidays(string) {
return "Happy holidays!"

}

function happyHolidaysTo(name) {
return ('Happy holidays, you!')

}

function happyHolidayTo(holiday, name){
return('Happy Independence Day, you!')
}

function holidayCountdown(holiday, days) {
return "It's ${days} days until ${holiday}!"
console.log('It's 20 days until Mother's Day!')

}

ReferenceErrors

I have defined the functions correctly in index.js but test cases turns
ReferenceErrors for functions, as not defined

Why is this failing the test? (solved)

Code appears to be correct according to instructions:

lean// write your code below!

function happyHolidays() {
  return "Happy holidays!";
}

function happyHolidaysTo(name) {
  return 'Happy holidays, ${name}!';
}

function holidayCountdown(days, holiday) {
  return 'It\'s ${days} days until ${holiday}!';
}

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.