Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODE_STYLE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

30 changes: 7 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
# Coursework

Like learning a musical instrument, programming requires daily practise.

The exercises are split into three folders: `exercises`, `mandatory` and `extra`. All homework in the `exercise` and `mandatory` section **must** be completed for homework by the following lesson.

The `extra` folder contains exercises that you can complete to challenge yourself, but are not required for the following lesson.

## Running the code/tests

The files for the mandatory/extra exercises are intended to be run as jest tests.

- Once you have cloned the repository, run `npm install` once in the terminal to install jest (and any necessary dependencies).
- To run the tests for all mandatory/extra exercises, run `npm test`
- To run only the tests for the mandatory exercises, run `npm test -- --selectProjects mandatory`
- To run only the tests for the extra exercises, run `npm test -- --selectProjects extra`
- To run a single exercise/test (for example `mandatory/1-writer.js`), run `npm test -- --testPathPattern mandatory/1-writer.js` (Remember, you can use tab-completion to get files relative to the current directory, so m`Tab ↹`/1-`Tab ↹` will autocomplete get you the test file starting with 1-)

For more information about tests, look here:

https://syllabus.codeyourfuture.io/guides/intro-to-tests

Try out variant way of running tests:

- `npm test` -> run all mandatory and extra tests
- `npm test -- --selectProjects mandatory` -> run only mandatory tests
- `npm test -- --testPathPattern mandatory/1-syntax-errors.js` -> run single test

## Solutions

The solutions for this coursework can be found here:

https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week1-Solution
https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week2-Solution

This is a **private** repository. Please request access from your Teachers, Buddy or City Coordinator after the start of your next lesson.

## Testing your work

- Each of the *.js files in the `exercises` folder can be run from the terminal using the `node` command with the path to the file. For example, `node exercises/B-boolean-literals/exercise.js` can be run from the root of the project.
- To run the tests in the `mandatory` folder, run `npm run test` from the root of the project (after having run `npm install` once before).
- To run the tests in the `extra` folder, run `npm run extra-tests` from the root of the project (after having run `npm install` once before).

## Instructions for submission

For your homework, we'll be using [**test driven development**](https://medium.com/@adityaalifnugraha/test-driven-development-tdd-in-a-nutshell-b9e05dfe8adb) to check your answers. Test driven development (or TDD) is the practice of writing tests for your code first, and then write your code to pass those tests. This is a very useful way of writing good quality code and is used in a lot of industries. You don't have to worry about knowing how this works, but if you're curious, engage with a volunteer to find out more! :)
Expand Down
66 changes: 66 additions & 0 deletions exercises/A-expressions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
In JavaScript there are **expressions** and **statements**. We will use these words frequently to describe code.

### Expression

An expression returns a value. Sometimes we will say that an expression _evaluates to_ a value.

The following are all examples of expressions:

```js
1 + 1; // returns 2
("hello"); // returns "hello"
2 * 4; // returns 8
"hello" + "world"; // returns "helloworld"
```

We can take the value produced by an expression and assign it to a variable. That line of code would be called a statement.

### Statement

A statement is some code that performs an action. Here are some examples:

```js
let sum = 1 + 1; // action: assigns result of `1 + 1` to variable `sum`
let greeting = "hello"; // action: assigns result of the expression "hello" to variable `greeting`
console.log(2 * 4); // action: logs the result of `2 * 4` to the console
sayGreeting(greeting); // action: calls the function `sayGreeting` with the parameter `greeting`
```

There are some other different types of statements that we will learn in the coming weeks.

## Exercise

You quickly find out the result of an expression by running node in a terminal window.

- Open a terminal window
- Run the command `node`
- _You have now opened a node console (also called a REPL)_
- Type an expression and press enter
- To exit the console type Ctrl+C or type the command `.exit`

Example from inside a terminal window:

```bash
$ node
> 1 + 2
3
> "hello"
'hello'
> let greeting = "hello"
undefined
> greeting
'hello'
> console.log(greeting)
hello
undefined
> .exit
$
```

> Notice how when we execute an expression the value it produces is printed below it. When we execute a statement, we see `undefined` printed below. This is because statements don't produce values like expressions, they _do something_.

- Write some more expressions in the node console
- Assign some expressions to variables
- Check the value of the variables

Further reading on using the node console: https://hackernoon.com/know-node-repl-better-dbd15bca0af6
9 changes: 9 additions & 0 deletions exercises/B-boolean-literals/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
There is a special data type in JavaScript known as a **boolean** value. A boolean is either `true` or `false`, and it should be written without quotes.

```js
let codeYourFutureIsGreat = true;
```

## Exercise

Head over to `exercise.js` and follow the instructions in the comments.
29 changes: 29 additions & 0 deletions exercises/B-boolean-literals/exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
BOOLEAN LITERALS
----------------
This program needs some variables to log the expected result.
Add the required variables with the correct boolean values assigned.
*/

let codeYourFutureIsGreat = true;
let mozafarIsCool = false;
let calculationCorrect = true;
let moreThan10Students = false;

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */

console.log("Is Code Your Future great?", codeYourFutureIsGreat);
console.log("Is Mozafar cool?", mozafarIsCool);
console.log("Does 1 + 1 = 2?", calculationCorrect);
console.log("Are there more than 10 students?", moreThan10Students);

/*
EXPECTED RESULT
---------------
Is Code Your Future great? true
Is Mozafar cool? false
Does 1 + 1 = 2? true
Are there more than 10 students? false
*/
4 changes: 3 additions & 1 deletion exercises/B-hello-world/exercise.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
console.log("Hello world");
console.log(
"Hello world i just started Learning Javascript home you welcome me"
);
29 changes: 29 additions & 0 deletions exercises/C-comparison-operators/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
We can also write **expressions** that return boolean values.

Here's an expression that evaulates to a boolean.

```
1 > 2
```

* Can you work out what value this expression evaluates to?

The `>` symbol in the expression is a **comparison operator**. Comparison operators compare two values. This operator checks to see if the number on the left is bigger than the number on the right.

`1` is not bigger than `2` so this expression returns `false`.

**More comparison operators**

```
> greater than
< less than
<= less than or equal
>= greater than or equal
=== same value
!== not the same value
```

## Exercise

* Open `exercise.js` and follow the instructions.
* Open a node console, and write some expressions that use comparison operators
35 changes: 35 additions & 0 deletions exercises/C-comparison-operators/exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
BOOLEAN WITH COMPARISON OPERATORS
---------------------------------
Using comparison operators complete the unfinished statements.
The variables should have values that match the expected results.
*/

let studentCount = 16;
let mentorCount = 9;
let moreStudentsThanMentors = true; // finish this statement

let roomMaxCapacity = 25;
let enoughSpaceInRoom = true; // finish this statement

let personA = "Daniel";
let personB = "Irina";
let sameName = false; // finish this statement

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
console.log("Are there more students than mentors?", moreStudentsThanMentors);
console.log(
"Is there enough space in the room for all students and mentors?",
enoughSpaceInRoom
);
console.log("Do person A and person B have the the same name?", sameName);

/*
EXPECTED RESULT
---------------
Are there more students than mentors? true
Is there enough space in the room for all students and mentors? true
Do person A and person B have the the same name? false
*/
4 changes: 3 additions & 1 deletion exercises/C-variables/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Start by creating a variable `greeting`

let greeting = "Hello World";
console.log(greeting);
console.log(greeting);
console.log(greeting);
39 changes: 39 additions & 0 deletions exercises/D-logical-operators/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
There are three logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT).

They let you write expressions that evaluate to a boolean value.

Suppose you want to test if a number if bigger than 3 and smaller than 10. We can write this using logical operators.

```js
let num = 10;

function satisfiesRequirements(num) {
if (num > 3 && num < 10) {
return true;
}

return false;
}
```

We can test expressions with logical operators in a node console too:

```sh
$ node
> let num = 10;
undefined
> num > 5 && num < 15
true
> num < 10 || num === 10
true
> false || true
true
> !true
false
> let greaterThan5 = num > 5
undefined
> !greaterThan5
false
> !(num === 10)
false
```
39 changes: 39 additions & 0 deletions exercises/D-logical-operators/exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Logical Operators
---------------------------------
Using logical operators complete the unfinished statements.
The variables should have values that match the expected results.
*/

// Do not change these two statement
let htmlLevel = 8;
let cssLevel = 4;

// Finish the statement to check whether HTML, CSS knowledge are above 5
// (hint: use the comparison operator from before)
let htmlLevelAbove5 = htmlLevel >= 5;
let cssLevelAbove5 = cssLevel > 5;

// Finish the next two statement
// Use the previous variables and logical operators
// Do not "hardcode" the answers
let cssAndHtmlAbove5 = htmlLevel > 5 && cssLevel > 5;
let cssOrHtmlAbove5 = cssLevel > htmlLevel > 5 || htmlLevel > 5;

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */

console.log("Is Html knowledge above 5?", htmlLevelAbove5);
console.log("Is CSS knowledge above 5?", cssLevelAbove5);
console.log("Is Html And CSS knowledge above 5?", cssAndHtmlAbove5);
console.log("Is either Html or CSS knowledge above 5?", cssOrHtmlAbove5);

/*
EXPECTED RESULT
---------------
Is Html knowledge above 5? true
Is CSS knowledge above 5? false
Is Html And CSS knowledge above 5? false
Is either Html or CSS knowledge above 5? true
*/
53 changes: 53 additions & 0 deletions exercises/D-logical-operators/exercise2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Logical Operators
---------------------------------
This program calls some functions that are either missing or incomplete.
Update the code so that you get the expected result.
*/

function isNegative(number) {
if (number < 0) {
return true;
} else {
return false;
}
}

function isBetween5and10(number2) {
if (number2 >= 5 && number2 <= 10) {
return true;
} else {
return false;
}
}

function isShortName(name) {
let isNameShort = new RegExp(/^([a-z]?$)/i);
return isNameShort.test(name);
}

function startsWithD(daniel) {
let nameStartD = daniel[0];
let result = nameStartD == "D";
return result;
}

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */

console.log("Is -10 is a negative number?", isNegative(-10));
console.log("Is 5 a negative number?", isNegative(5));
console.log("Is 10 in the range 5-10?", isBetween5and10(10));
console.log("Is Daniel a short name?", isShortName("Daniel"));
console.log("Does Daniel start with 'D'?", startsWithD("Daniel"));

/*
EXPECTED RESULT
---------------
Is -10 is a negative number? true
Is 5 a negative number? false
Is 10 in the range 5-10? true
Is Daniel a short name? true
Does Daniel start with 'D'?
*/
3 changes: 3 additions & 0 deletions exercises/D-strings/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Start by creating a variable `message`
var message = "This is a string";
var messageType = typeof message;

console.log(message);
console.log(messageType);
Loading