Git Product home page Git Product logo

js-if-else-files-lab's Introduction

Using Conditionals and Multiple Files in JS

Introduction

We now know how to use conditionals. Now, let's use conditionals to test our code for correctness. This is your first step towards how professional developers write software. Professional developers test their code for correctness. Instead of constantly clicking around their applications, most developers write additional code, called tests that ensure their code is outputting the right things. In fact, they may even write tests first before they actually do any work towards a project to help ensure the code they write does what they expect it to do!

We will eventually explain how to use the professional testing tools but first, let's go over a simple example.

Instructions

After you open this lesson in the IDE, in the js folder, double-click on index.js. You'll see some basic code:

// ./js/index.js

var name = "Joe"
var height = "74"

// Don't worry about this
module.exports = { name, height
}

First of all, don't worry about the module.exports stuff right now, we'll get to that later. You've seen the rest of this stuff before but let's review. This assigns "Joe" to the name variable using the assignment operator (=). It also assigns "74" to height. Both "Joe" and "74" are Strings. We know that because they are wrapped in quotes. We have our code, but we don't have our tests. Let's write a test right below the var height = "74" line to check that name is equal to "Susan". That should look something like this:

if (name === "Susan") {
    console.log("The name is correct")
}

Write that, and then run your index.js file with node js/index.js in the terminal. You'll see nothing printed out. That's because our name variable doesn't equal "Susan". Let's put in an else statement that prints "Expected the name to be Susan". Give it a try on your own.

OK, you gave it a try, now let's look at the solution:

if (name === "Susan") {
    console.log("The name is correct")
} else {
    console.log("Expected the name to be Susan")
}

See what we did there? Great! Now run your code again by typing node js/index.js in the terminal and you should get Expected the name to be Susan printed out. Let's fix our name variable to equal "Susan". Modify the first line to say:

var name = "Susan"

Re-run your code and boom! you did it :)

In later steps, we'll be doing our testing in other files. So, for now, let's revert index.js to its original state. Make sure the index.js file is saved and that it looks like this:

var name = "Joe"
var height = "74"

// Don't worry about this
module.exports = { name, height
}

Separating Tests from Application Code

As you can probably imagine, a large application will have many tests. Average sized applications will have thousands of tests. It gets a bit confusing to have the application code mixed in with your test code. We want to separate them out so it's not a pain to sift through our files.

Let's open up another JavaScript file and play around with separating things out. There is a blank JS file included in this lab called other_file.js located in the js folder. Double click on that and you'll see a blank file. Let's say we wanted to console.log the name variable from our index.js file. Seems fairly easy. Go ahead and type console.log(name). Now let's run this new file by typing node js/other_file.js in our terminal. BOOM! You'll get an error that looks like this:

/home/jmburges/code/labs/js-functions-lab/test.js:1
(function (exports, require, module, __filename, __dirname) { console.log(name)
                                                                          ^

ReferenceError: name is not defined
    at Object.<anonymous> (/home/jmburges/code/labs/js-functions-lab/test.js:1:75)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:393:7)
    at startup (bootstrap_node.js:150:9)
    at bootstrap_node.js:508:3

The important line here is the ReferenceError: name is not defined line. That says that our name variable doesn't exist in the other_file.js file. That makes sense. In other_file.js we never write the line of code to create name. We have the line var name = "Joe" in index.js. We need to tell other_file.js about the existence of index.js! Let's do that by replacing the contents of other_file.js with the following line of code:

var index = require("./index.js")

This tells javascript to load the index.js file and put its contents inside of the index variable within other_file.js. That's a bit confusing, but it's fairly straight forward to use. Thanks to require(), we now have access to the variables we exported from index.js, including name. To access name within other_file.js, we can simply refer to the name property of the index variable, which, again, is where we used require() to store the contents of index.js. Let's add the following line to other_file.js:

var index = require("./index.js")

console.log(index.name)

Give that a run by typing node js/other_file.js and you should see the name getting printed out. Awesome!

Your Turn

You now know how multiple files interact as well as how if statements work. Now, you have to write your code to match some specific tests. Open up js/tests.js and you will see two if statements. Let's give this a run to start things off by typing node js/tests.js. You should get two messages:

Expected: Susan, Received: Joe
Expected: 70, Received: 74

Now, it's your job to modify index.js so that when you run node js/tests.js the messages you see logged to the console are:

The name is correct
The height is correct

Note: Capitalization matters. Also, whether or not you use a String or Number matters. Numbers don't have any quotes around them, Strings do have quotes around them!

When you've finished your solution, and the tests show the output above, run learn submit from the console to submit your solution.

js-if-else-files-lab's People

Contributors

dakotalmartinez avatar gj avatar jmburges avatar lizbur10 avatar maxwellbenton avatar rrcobb avatar sgharms avatar tmtarpinian avatar

Stargazers

 avatar  avatar

Watchers

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

js-if-else-files-lab's Issues

Deleting console.log(name)

when importing the other line of code into the blank file, I am following along exactly. I keep getting an error when I am told to type console.log(index.name). The error didn't change from the previous, purposeful error. I am very new, as I am sure a lot of others are, so I follow exactly. It never says to define "name" or delete console.log(name). These things are so frustrating, that I have almost quit the school 3 times.

This was an issue following the lessons exactly, 2 lessons back, I was never told or explained to that I had to save after every single change, in order for my codes to run and not get an error.

These 2 problems have cost me more than 2 hours of trying to figure out what I am doing wrong and how to fix it.
2017-06-22 1
2017-06-22

No IDE button to open the IDE (instead, "SANDBOX")

Even though the lesson states "After you open this lesson in the IDE...", there isn't actually an IDE button on this page. Only a SANDBOX button which doesn't contain the files and text the lesson mentions.

Learn IDE interface problem.

Basically the LearnIDE program opens, however when you try to choose any file in any of the directories, the Learn IDE interface does not work.

height number in tests.js is not in quotations

not sure if intended but the test for height does not have quotations around it leading to an error. this could be a test of attention to detail, but just wanted to raise the issue if its an error.

Files

In my IDE the proper files are not showing making it a strange lab to complete.

Practicing Conditional Statement Not Submitting/ Missing button

Hello,

My name is Jonathan Harris and I am a part of the online curriculum. For some reason as I am trying to submit the file for submission it is not allowing me to submit the file. I have completed the lab, but am unable to submit my thread to continue properly. What can I do to complete learn submit.

Best,

Jonathan Harris
Online Student

Tests.js Results

When running the tests.js portion it tells me that I should receive two lines one stating that it received Joe as an answer and the other stating that it received the wrong height. Because I was told to change the name to Susan earlier on and not told to change it back to Joe this is not the results I received.

No IDE

Is this lesson supposed to have an IDE, because all I am getting is the Sandbox.

conditionals

i did the lab right but somehow it does not let me submit it.

Missing comma, perhaps

Slight thing, but just wanted to say you might be missing a comma after "called tests" in the following sentence:

Instead of constantly clicking around their applications, most developers write additional code, called tests that ensure their code is outputting the right things.

I believe it should read as:
Instead of constantly clicking around their applications, most developers write additional code, called tests, that ensure their code is outputting the right things.

It's a really small thing, but just for a matter of correctness. I'm fine with being wrong, lowkey expecting to be, but that's that.

There is no IDE in this lesson available to open

There is no IDE in this lesson available to open, instead sandbox appears to have replaced it as the option for using the developer tool. And, it is impossible to change directory from sandbox to js-if-else-files-lab-bootcamp-prep-000/index.js because they are not in the same file tree. Since the IDE is required to fully comprehend this lesson, it would be great if this could be fixed as soon as possible. If I am misunderstanding how this lab is supposed to work, please notify me so as to correct user error.

Sincerly,
Wyatt Massey

Practicing Conditional Statements index.js

Attached is a screenshot of the IDE. Note the exclamation point by var name. When I hover over it, it says "Redefinition of 'name'. Missing semicolon". Is anyone able to explain that? It is preventing me from continuing the lab. Thank you! I'm sure it is incredibly basic but I am very new :)

Screen Shot 2019-07-23 at 7 21 25 PM

I think there is an error in the instructions, Or is this intentional?

Hello.
I am working my way through this lesson. I am to the part where the error is generated in the other_file.js after its first run. There is the line in the text that reads "We have the line var name = "Susan" in index.js.".
This is after the earlier instructions have us restore the index.js file to its original state "So, for now, let's revert index.js to its original state. Make sure the index.js file is saved and that it looks like this:". At this point variable "name" is set to "Joe"

Rob

Asking a Question

Im trying to ask a question about running my code in the terminal and it wont give me the option to ask the question.

confusing wording

at the end of the lesson we are told to modify index.js so that ---

"when you run tests.js it says you got both height and name"

this is a confusing statement. what does "got" mean? got what? the contents of the height and name variable? just them? or also the prompt that says our input is correct? hopelessly unclear. please reword to be clearer about what you're asking for.

Misleading directions since this lab doesn't have an open IDE option

The readme states: "After you open this lesson in the IDE, double-click on index.js. You'll see some basic code:"

However, this lab only has the option of opening the Sandbox. Many students ask about this and are confused because of this. I would suggest either switching it back to a lab with the IDE or providing clear instructions about how to clone the lab into the sandbox.

@DanielSeehausen

Had to manually grab files from github

During this lesson, the open IDE button was not present only "open sandbox".
I was able to complete the lesson via creating my own files in the sandbox and copy pasting code from github manually.

Not seeing IDE...Missing Files

Wondering where the rest of the IDE files are...All I can see in the Sandbox is READEME.md. The way the narrative reads, I should have the option for the IDE and see an assortment of files. Please advise.

There is no Open IDE button: change lab directions

The directions say: After you open this lesson in the IDE, double-click on index.js. You'll see some basic code:

Make it clear that students need to manually fork and clone from github to see the files referred to.

Unclear directions

For the lesson "Practicing Conditional Statements" -
screen shot 2017-10-12 at 9 58 29 am
In order to get the correct output in the terminal, the index.js file must be saved.
In the next section, "Separating tests from application code," the intended output ("Give that a run by typing nodejs other_file.js and you should see the name getting printed out.") assumes the original file is being run, not the saved version. The saved version with the if/else statement outputs:
The name is correct
Susan

The final exercise, "Your turn," also assumes the index.js file has been returned to the original state.

The readme should clarify when to save/revert to the original index.js document, unless you meant to create a problem just to see if students can figure it out. If so, well done. As you were.

Functions lab

The code I have written is almost verbatim the same as the example in the lesson. 3 people have tried to help me in the chat. No luck.

Open IDE

There is no button to open the IDE in the lesson "practice conditional statements." the Sandbox just has a readme file

cannot submit lab

I have completed this lab twice - the second time I logged out and re-logged in and walked through the lab and I still can not submit or mark as read. When I try to move on to the next lesson I get an error message saying that I am going out of order

update: closing issue, I just needed to sumbit from terminal eyeroll sorry!

Sandbox not IDE appears on button in this lab/lesson

In the Practicing Conditional Statements lesson in Bootcamp prep. the lesson text says to use the IDE but the button to open the IDE is linked to the Sandbox instead (see attached PNG file). I had installed the Learn IDE 3 per instructions. Rebooted computer. Still got the Sandbox button. I'm using Windows 7 OS, and Chrome browser. In other lessons I correctly see the IDE button for labs where we're supposed to use it.

I did a workaround by forking & cloning the lab from Github, but wonder if there's a tech issue with this lesson? Just wanted to let you know! Thanks!

lesson_conditionals_sandbox-error

no tests.js

Upon opening the in-browser IDE (technically Sandbox), the file directory was empty. Was able to create a few js files to start the lab, however without tests.js, was not able to complete the lab.

Mr

while doing js.index conditonal statement
var name = "Joe"
var height = "74"
then when I go for testing nodejs index.js
I get the error DIRECTORY DOES NOT APPEAR TO HAVE SPECS "
what it is

Lesson not saving as completed

This lesson states that it is finished on page but does not save as finished so it is preventing my curriculum from advancing. Perhaps this is a bug for multiple users?

Having trouble passing this lab

Really need someone to screen share with me to pass this lab. It happens when i type in
nodejs index.js. I recieve this:
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:383:7)
at startup (bootstrap_node.js:149:9)

Not sure what this means>

Conditionals lab expected output

In other_file.js I've written the following, according to the lab:

var index = require("./index.js")

console.log(index.name)

At this point in the lab I get the following output when I run the nodejs other_file.js command:

The name is correct
Susan

My expected output was this:

Susan

Is it normal or otherwise to be expected for the log function to also run the conditional statement in index.js? If I comment out the entire conditional statement using /**/ I do not get the printed outputs from the conditional statement in index.js when I run nodejs other_file.js.

Not showing "Open IDE"

Hi - This part of the curriculum is not showing an "Open IDE" for me to practice conditional statements. I only see a button for Sandbox. Is there another way to complete this lesson's task?

Name="Susan"

I can't set the variable name="Susan"
There is a warning that keeps appearing and prevents me from completing the lesson.

Load Issue

No matter what I try to do I continue to try and open the IDE project and it fails every time. It fails to open the project each time I try. How can I continue if the program doesnt work.

Your Turn

In the last section of the lesson, it asks you to modify index.js so when you run test.js it shows you have both height and name. If the results are supposed to reflect the bottom image, is the expectation to replace "Susan" with "Joe" in index.js?

Previously, we found the error in the lesson text where it said "Joe" when it should have said "Susan" so just trying to make sure that this was the direction and not a typo.
screen shot 2017-06-29 at 4 29 03 pm

if else files lab

I'm not sure the lab is going the way its supposed to. There is a yellow triangle next to the 'var name' saying "Redefinition of 'name' missing semi-colon". I'm getting different results that what the examples are showing me.

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.