Giter Site home page Giter Site logo

javascript-strings-lab's Introduction

JavaScript Strings Lab

lab

Overview

In this lab, we're going to work with strings. Strings in JavaScript are wrapped in single or double quotes, or in back ticks.

Objectives

  1. Manipulate strings in JavaScript
  2. Practice interpolating with template literals

Introduction

Imagine we're planning a birthday party for Bill Nye. There are going to be a lot of people there, so we're going to use JavaScript to help us keep everything straight.

First, we need to practice greeting everyone. (I don't know about you, but I sometimes get nervous and say the dumbest things — but we don't want to embarrass ourselves in front of Bill Nye!)

One might think that we could just type

Hello, everybody!

in our browser's console and be done with it. Give it a try. (If you're on a Mac, that would be Command + Option + J together.)

You should see something like

Uncaught ReferenceError: Hello is not defined(…)

Well, that won't work. (This is why we practice!) In order to greet our guests, we need to tell JavaScript that we're using a string. A string is a collection of characters (letters, numbers, and symbols) wrapped in single or double quotes (or, as we'll see, in back ticks). So to greet everyone, we can write,

'Hello, everybody!'

or

"Hello, everybody!"

Single or double quotation marks can contain a string variable..

What if we want to say hi to a special guest, like Neil deGrasse Tyson? When we wrap strings in single or double quotes, we can join them together using the + operator:

var specialGuest = "Neil deGrasse Tyson"
"Hello, " + specialGuest + "!" // "Hello, Neil deGrasse Tyson!"

This is called concatenation. Notice that the value of the specialGuest variable is also a string!

TOP TIP: Your console might be getting a little full at this point. If at any point you'd like to clear it out and start fresh, you can either click the button in the top left corner of the console — in Chrome, it looks like this:

clear console

Alternatively, you can press ctrl + L or command + K. As long as you don't refresh the page, anything you've declared will stick around for you to reference — you'll just get a nice blank slate on which to code.

When we wrap strings in back ticks, we can use placeholders (${}) and insert variables or evaluated JavaScript directly:

var specialGuest = "Neil deGrasse Tyson";

`Hello, ${specialGuest}! High ${3 + 2}!` // "Hello, Neil deGrasse Tyson! High 5!"

This is called interpolation.

Lab

You'll find a file called index.js in this directory. Your mission, should you choose to accept it, is to get its tests (in tests/index-test.js) to pass.

You can run the tests using the learn command in your terminal or the Learn IDE. Give that a go now.

waiting

All three tests have failed! This is okay, and it's expected — you haven't written any code yet, after all.

In index.js, you'll see five lines of code:

var greeting = "";

var specialGuest = "Neil deGrasse Tyson"

var greetSpecialGuest = "" + specialGuest + "!";

var topic = "space";

var conversation = `${topic}`;

Each line has a test associated with it. When the tests fail, they show us what the expected value is — your job is to make that expectation a reality by modifying the code provided.

When you first run learn, you should see something like this:

All tests failing.

Let's walk through that first error together. First, we see the test title:

1) strings defines `greeting`:

The title tells us what the test expects our code to do. In this case, "strings" refers to the general problem space in which we're working — we're handling strings.

Continuing on with the test output, we can now make better sense of the next few lines:

AssertionError: '!' == 'Hello, everybody!'
+ expected - actual

- !
+Hello, everybody!

This is a lot to take in, so we'll go through it slowly.

What could AssertionError mean? Well, it probably means that our test asserted (or expected) that something would be true, and that thing wasn't true.

What is that thing? The test expected the empty string, '', to be equal to the string 'Hello, everybody!' — but, of course, these strings are not equal.

+ expected - actual is a key for reading the statements below it. + expected tells us that the expected output shows up in that yellowish green; - actual tells us what actually happened.

But reading on, we only see +Hello, everybody! — what's going on? Why isn't there any - actual output? Well, there was no actual output — it's just an empty string! That must be the problem!

Next, the title tells us that index.js "defines greeting." Let's look in index.js — sure enough, we see, at the top of the file, var greeting = "";. Seems like a reasonable place to start.

What if, instead of assigning "" to greeting, we assign "Hello, everybody!", like the test expects. Go ahead and change that line in index.js so it reads

var greeting = "Hello, everybody!";

save the file, and rerun your tests. You should see

First test passes!

Nice! You got the first test to pass.

Now use the skills that you learned above to read through the rest of the test output and fix those errors, too! Always remember to save your file before re-running your tests.

NOTE: Because we're dealing with some low-level language features, you might spot some easy ways to "cheat" on this lab, or this lab might seem frustratingly easy. We've given you some starter code to point you in the right direction — try to solve the lab as intended! You can then compare your solution with ours (found in the solution branch of this repository).

When your tests are passing, submit your answer by typing in learn submit in the command line or else create a pull request (use learn submit if "pull request" sounds a bit terrifying).

Good luck!

View JavaScript Strings Lab on Learn.co and start learning to code for free.

javascript-strings-lab's People

Contributors

7kingdavid7 avatar alpha-convert avatar annjohn avatar captrat avatar dhumilz avatar emilycroft avatar gj avatar ivalentine avatar jeffkatzy avatar jessrudder avatar mendelb avatar mobesa avatar pletcher avatar vivshaw avatar

Watchers

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

javascript-strings-lab's Issues

Bug with moving forward

I used "learn submit" in IDE and it went through, but I can't move forward with my lesson in learn.

All tests are failing

When I ran this, I got 0 tests passing and 3 failing

screen shot 2016-06-16 at 10 30 01 am

Readme says that 1 should be passing and that there should be 4 tests in all.

@jmburges

Funky error! Please help!

Using this line of code

var topic = Let's talk about space;

var conversation = ${topic};

Getting this error:

  1. interpolates a string in conversation:

    Error: Expected 'Let's talk about space' to equal 'Let's talk about Let's talk about space.'

    • expected - actual

    -Let's talk about space
    +Let's talk about Let's talk about space.

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

Learn not presenting code

I am trying to start the first test in my technical requirement but Atom is not pulling the code like it has done previously. Please advise.

Questions

I think this is a good lab, but the one issue here is that we haven't really covered variables yet. We also haven't taught them what error messages and tests here.

Because this lab is not very complicated, this could be the best place to explain error messages and tests. Maybe you can walk them through the first error, and very handholdedly tell them what the test is saying and show them what they should do. And have them run the tests again and they'll see that it passes. This is also a good place to describe the different parts of an error message.

And then once they get that, then that's encouragement for them to continue with the next three tests in the lab.

learn

I'm not sure I understand how to run the learn IDE? It's not clear to me. Thanks!

Expected Test Result Changes

Final test "Let's talk about space." continuously fails because the expected result changes after multiple passes.

screenshot 2017-07-08 12 57 47

Then, after modifying the var, this is the test result
screenshot 2017-07-08 12 59 21

open ide does nothing

Not sure what im really supposed to be working in when i get to the Lab section, tried clicking open ide to see if that gave the index.js and test file to work in but it does nothing

IDE learn never works

I am really glad I haven't paid money for this because I would be livid! NONE of the labs run and the f-----g IDE learn doesn't even open half the time. I'm glad this course is free, but it is a total waste of time! And I can never get any help of resolve any of this. If I go to a bootcamp, it sure as F--- is not going to be you guys!

Running test in IDE, cannot find preset "es2015"

Get this error when trying to run this lab in the Learn IDE:

1)  "before all" hook:                                    
     Error: Couldn't find preset "es2015" relative to directory "/path/to/lab/javascript-strings-lab-js-intro-000"  

(I edited out path/to/lab here^)

This can be fixed by running npm install babel-preset-es2015 --save-dev to add this package to the package.json.

@AnnJohn

Mocha Test Frozen

Hello! I'm having issues running tests on the Beatles lab because the Mocha Test software keeps freezing after I try to run the second function. Should I proceed to complete the lab and turn it in without testing it or should I wait for technical assistance?

P.S. I had this same problem on this same lab when I was going through the tutorials

Thank you for any help you can provide!

Not able to Open IDE

Hi,

I am not able to open IDE. On clicking the Open IDE nothing happens, Page gets refreshed. I am using Using Google Chrome. Please support.

Regards,
Upasna Langar

Lesson Text is doubling following test

Every time I correct the strings and do a test it tells me that I need to repeat the corrections that I've just made. This leads me to typing in strings such as "Hello, Hello, Hello, Hello, Neil deGrasse Tyson!!" I think this lesson has an error or the IDE has a bug. If I were to type in the text that the IDE expects it will just tell me to double the text necessary to pass the test again like this: "Hello, Hello, Hello, Hello, Hello, Neil deGrasse Tyson!!!" Here is a picture of the results:
image

Function not working

When i write this into the console i:
`function sayHelloTo(firstName) {
console.log('Hello, ${firstName}!')
}

sayHelloTo('Kagemanden')`

Get this output:
Hello, ${firstName}!

intro to js: test output doesn't match curriculum

when running learn, I see AssertionError: '!' == 'Hello, everybody!' but curriculum is talking about AssertionError: '' == 'Hello, everybody!'

No '!', it's talking about an empty string.

Goes on saying. What is that thing? The test expected the empty string, '', to be equal to the string 'Hello, everybody!' — but, of course, these strings are not equal.

NOTE: The screenshot provided does show AssertionError: '!' == 'Hello, everybody!'

Not sure which is correct.

Cannot fork or submit

I have been able to correct all the code and I pass the tests. However, the 'Fork' light or the 'Submit' light on learn.co do not turn green and I cannot proceed.

Thank you

Typo?

Are "evaluated" and perhaps the language around it a typo in the following sentence?: "When we wrap strings in back ticks, we can use placeholders (${}) and insert variables or evaluated JavaScript directly:"

Lesson directory is missing. Can't fully complete lesson.

Hi there,

I was doing the lesson for the javascript-strings-lab but I'm not able to see the directory in the IDE. I've tried opening/reinstalling the lesson but files still don't show up. Is there a way to reset the lesson? I think it may have something to do with that or something in my cache.

string lab

Thanks for the assistance.

-A

Strings Lab

Yes thanks. Please close earlier one. Again I am not quite sure the code for correcting errors 2 and 3. Many thanks. Chris

intro to js: No var called myString in string.js

Lab says:
Next, the title tells us that strings.js "defines myString." Let's look in strings.js — sure enough, we see, at the top of the file, myString = "";. Seems like a reasonable place to start.

but in the repo, there is not variable called myString.

Curriculum <> IDE Out of Sync

The curriculum asks for us to run the placeholder Java code in the first lab prior to beginning to make any changes, then proceeds to walk through the error messages to explain what is happening. There appears to be an issue with the description of the error messages in the curriculum which does not align with the error messages actually being generated. Here's the curriculum section on the error message for the first condition:
Screen Shot 2020-01-27 at 11 46 20 PM

Here is the actual error message output by the IDE terminal:
Screen Shot 2020-01-27 at 11 47 21 PM

Terminal

My lesson files are not loading in the terminal

test again

This issue is really terrible. I can't follow it at all.

`==` has not yet been explained.

@pletcher near the bottom of this lab where you are explaining the test, you say "(remember ==?)". I took a quick look back, and I don't think that == has been explained yet.

learn submit

learn submit not working
it push changes but in progress bar "submit pull" showing as not don

Lab isn't working.

Hello!

I've added

function basicTeenager(age) {if (age >= 13 > && age <= 19) {return ("You are a teenager!")}

but I'm still getting the error message:

  1. flow-control basicTeenager should return "You are a teenager!" if the age is between 13-19:
    Error: Expected undefined to equal 'You are a teenager!'
    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/flow-control-test.js:26:33)

Not lesson specific - clear console

Is it worth mentioning how to clear the console? Whether using Cntrl L or the left button? I know people will dive deeper into dev tools on their own time, but students are doing so much testing in the console and they could get intimidated by content in the console on page load.

JavaScript Strings Lab - text correction

In the paragraph below:

All three tests have failed! This is okay, and it's expected — you haven't written any code yet, after all.
In index.js, you'll see four lines of code:

There are five lines of code instead of four as mentioned in the lesson.

Refer to interpolation and concatenation

We don't need to go over this again in this lab, but it's been a while since they saw interpolation and concatenation (relatively speaking) (and also from a previous isssue, I think we need to be a bit more explicit about interpolation in the lesson on data types).

But when we ask them to complete the rest of this lab, can we nudge them and say that they'll need to use interpolation and concatenation and that they should go back to Unit 1 if they need a refresher?

@jmburges

Unable to Complete Lab

I am unable to complete this lab due to a error message. I am also having trouble loading the other labs in the other lessons. This is the message I see.
screen shot 2017-03-18 at 12 16 10 pm

index.js and index-test.js not downloaded

Opened the lesson for strings lab and the index.js and index-test.js still contains the information I entered for the previous lab. If I run learn in the console it produces the correct errors for the lab and the errors refer to these two files but I can't edit them to complete lab.

my view

Re-doing my Intro to JavaScript

Intro to Java Strings Lab
I decided to erase everything, then try to do it again although I passed it 3 months ago.

var greeting = "Hello, everybody!";

var specialGuest = "Neil deGrasse Tyson!";

var greetSpecialGuest = "Hello," + specialGuest + "!";

var topic = "space";

var conversation =Let\'s talk about ${topic}.;

Line 3 seems to be failing. It's telling me I am missing a space after the "Hello,". Does anyone know how to re-set a lab?

Wrong Lab Opening

In this lesson when I click "open", the previous lab opens in Learn-IDE. It is not the lab that is supposed to be completed in this lesson.

Can someone else run the tests with the solution?

It might be just me, but when I solved the lab and ran the tests again, they weren't passing.

I also just copied the solution and ran tests and still got them failing. Have also been saving the files.

But again, could be something I'm doing wrong

@jmburges

Strings lab

For some reason everytime I attempt to solve the third test and input 'let's talk about space.' it gives back the error : expected=let's talk about let's talk about space..

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.