Skip to content
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
id: 647e214b8acb466c97ccbee0
title: 'Branch the Flow of Code Using the switch-case Construct in C#'
challengeType: 19
dashedName: branch-the-flow-of-code-using-the-switch-case-construct-in-c-sharp
---

# --description--

This challenge will be partially completed on the Microsoft Learn platform. Follow these instructions to complete the challenge:

1. Go to <a href="https://learn.microsoft.com/training/modules/csharp-switch-case/" target="_blank" rel="noreferrer">https://learn.microsoft.com/training/modules/csharp-switch-case/</a> and complete all the tasks for the "Branch the Flow of Code Using the switch-case Construct in C#" module. This is **required** to earn the "Add Logic to C# Console Applications" trophy on Microsoft Learn, and qualify for the certification exam.
1. When you are finished, come back and correctly answer the question below.

# --assignment--

Complete the <a href="https://learn.microsoft.com/training/modules/csharp-switch-case/" target="_blank" rel="noreferrer">Branch the Flow of Code Using the `switch`-`case` Construct in C#</a> module on Microsoft Learn. Then, answer the question below.

# --questions--

## --text--

What is the purpose of the `break` keyword?

## --answers--

The `break` keyword tells the runtime to continue evaluating other cases in the `switch` construct.

---

The `break` keyword tells the runtime to stop evaluating other cases in the `switch` construct.

---

The `break` keyword tells the runtime to exit the application.

## --video-solution--

2
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
id: 647e21598acb466c97ccbee1
title: 'Challenge Project - Develop Branching and Looping Structures in C#'
challengeType: 19
dashedName: challenge-project-develop-branching-and-looping-structures-in-c-sharp
---

# --description--

This challenge will be partially completed on the Microsoft Learn platform. Follow these instructions to complete the challenge:

1. Go to <a href="https://learn.microsoft.com/training/modules/challenge-project-develop-branching-looping-structures-c-sharp/" target="_blank" rel="noreferrer">https://learn.microsoft.com/training/modules/challenge-project-develop-branching-looping-structures-c-sharp/</a> and complete all the tasks for the "Challenge Project - Develop Branching and Looping Structures in C#" module. This is **required** to earn the "Add Logic to C# Console Applications" trophy on Microsoft Learn, and qualify for the certification exam.
1. When you are finished, come back and correctly answer the question below.

# --assignment--

Complete the <a href="https://learn.microsoft.com/training/modules/challenge-project-develop-branching-looping-structures-c-sharp/" target="_blank" rel="noreferrer">Develop Branching and Looping Structures in C#</a> challenge project on Microsoft Learn. Then, answer the question below.

# --questions--

## --text--

A developer needs to create an iteration statement. Under what condition is a `while` statement a better choice than a `do` statement?

## --answers--

When the expression values evaluated by the iteration statement are unknown prior to the iteration code block.

---

When the expression values evaluated by the iteration statement are known prior to the iteration code block.

---

When the developer creates nested `do` or `while` loops.

## --video-solution--

2
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
id: 647e21658acb466c97ccbee2
title: 'Control Variable Scope and Logic Using Code Blocks in C#'
challengeType: 19
dashedName: control-variable-scope-and-logic-using-code-blocks-in-c-sharp
---

# --description--

This challenge will be partially completed on the Microsoft Learn platform. Follow these instructions to complete the challenge:

1. Go to <a href="https://learn.microsoft.com/training/modules/csharp-code-blocks/" target="_blank" rel="noreferrer">https://learn.microsoft.com/training/modules/csharp-code-blocks/</a> and complete all the tasks for the "Control Variable Scope and Logic Using Code Blocks in C#" module. This is **required** to earn the "Add Logic to C# Console Applications" trophy on Microsoft Learn, and qualify for the certification exam.
1. When you are finished, come back and correctly answer the question below.

# --assignment--

Complete the <a href="https://learn.microsoft.com/training/modules/csharp-code-blocks/" target="_blank" rel="noreferrer">Control Variable Scope and Logic Using Code Blocks in C#</a> module on Microsoft Learn. Then, answer the question below.

# --questions--

## --text--

Which of the following statements is true about showing/removing the curly braces for code blocks associated with an `if` statement?

## --answers--

The curly braces can't be removed from the code block for `else if` and `else` statements.

---

If the curly braces are removed from the code blocks of an `if-elseif-else`, the white space must also be removed.

---

Always choose a style that improves readability.

## --video-solution--

3
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
id: 647e217e8acb466c97ccbee4
title: 'Guided Project - Develop Conditional Branching and Looping Structures in C#'
challengeType: 19
dashedName: guided-project-develop-conditional-branching-and-looping-structures-in-c-sharp
---

# --description--

This challenge will be partially completed on the Microsoft Learn platform. Follow these instructions to complete the challenge:

1. Go to <a href="https://learn.microsoft.com/training/modules/guided-project-develop-conditional-branching-looping/" target="_blank" rel="noreferrer">https://learn.microsoft.com/training/modules/guided-project-develop-conditional-branching-looping/</a> and complete all the tasks for the "Guided Project - Develop Conditional Branching and Looping Structures in C#" module. This is **required** to earn the "Add Logic to C# Console Applications" trophy on Microsoft Learn, and qualify for the certification exam.
1. When you are finished, come back and correctly answer the question below.

# --assignment--

Complete the <a href="https://learn.microsoft.com/training/modules/guided-project-develop-conditional-branching-looping/" target="_blank" rel="noreferrer">Develop Conditional Branching and Looping Structures in C#</a> guided project on Microsoft Learn. Then, answer the question below.

# --questions--

## --text--

When is it appropriate to use a `switch-case` construct rather than a `if-elseif-else` construct?

## --answers--

A `switch-case` construct is appropriate when more than 2-3 `else if` code blocks are required.

---

A `switch-case` construct is appropriate when the number of `case` patterns is small.

---

A `switch-case` construct is appropriate when the selection statement is inside a loop.


## --video-solution--

1
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
id: 5895f700f9fc0f352b528e63
title: Set up a Template Engine
challengeType: 2
forumTopicId: 301564
dashedName: set-up-a-template-engine
---

# --description--

Working on these challenges will involve you writing your code using one of the following methods:

- Clone <a href="https://github.com/freeCodeCamp/boilerplate-advancednode/" target="_blank" rel="noopener noreferrer nofollow">this GitHub repo</a> and complete these challenges locally.
- Use a site builder of your choice to complete the project. Be sure to incorporate all the files from our GitHub repo.

A template engine enables you to use static template files (such as those written in *Pug*) in your app. At runtime, the template engine replaces variables in a template file with actual values which can be supplied by your server. Then it transforms the template into a static HTML file that is sent to the client. This approach makes it easier to design an HTML page and allows for displaying variables on the page without needing to make an API call from the client.

`pug@~3.0.0` has already been installed, and is listed as a dependency in your `package.json` file.

Express needs to know which template engine you are using. Use the `set` method to assign `pug` as the `view engine` property's value:

```javascript
app.set('view engine', 'pug');
```

After that, add another `set` method that sets the `views` property of your `app` to point to the `./views/pug` directory. This tells Express to render all views relative to that directory.

Finally, use `res.render()` in the route for your home page, passing `index` as the first argument. This will render the `pug` template.

If all went as planned, your app home page will no longer be blank. Instead, it will display a message indicating you've successfully rendered the Pug template!

Submit your page when you think you've got it right. If you're running into errors, you can <a href="https://forum.freecodecamp.org/t/advanced-node-and-express/567135#set-up-a-template-engine-1" target="_blank" rel="noopener noreferrer nofollow">check out the project completed up to this point</a>.

# --hints--

Pug should be a dependency.

```js
async () => {
const url = new URL("/_api/package.json", code);
const res = await fetch(url);
const packJson = await res.json();
assert.property(
packJson.dependencies,
'pug',
'Your project should list "pug" as a dependency'
);
}
```

View engine should be Pug.

```js
async () => {
const url = new URL("/_api/app", code);
const res = await fetch(url);
const app = await res.json();
assert.equal(app?.settings?.['view engine'], "pug");
}
```

You should set the `views` property of the application to `./views/pug`.

```js
async () => {
const url = new URL("/_api/app", code);
const res = await fetch(url);
const app = await res.json();
assert.equal(app?.settings?.views, "./views/pug");
}
```

Use the correct ExpressJS method to render the index page from the response.

```js
async () => {
const url = new URL("/", code);
const res = await fetch(url);
const data = await res.text();
assert.match(
data,
/FCC Advanced Node and Express/gi,
'You successfully rendered the Pug template!'
);
}
```

Pug should be working.

```js
async () => {
const url = new URL("/", code);
const res = await fetch(url);
const data = await res.text();
assert.match(
data,
/pug-success-message/gi,
'Your projects home page should now be rendered by pug with the projects .pug file unaltered'
);
}
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
id: 5895f70cf9fc0f352b528e65
title: Set up Passport
challengeType: 2
forumTopicId: 301565
dashedName: set-up-passport
---

# --description--

It's time to set up *Passport* so you can finally start allowing a user to register or log in to an account. In addition to Passport, you will use Express-session to handle sessions. Express-session has a ton of advanced features you can use, but for now you are just going to use the basics. Using this middleware saves the session id as a cookie in the client, and allows us to access the session data using that id on the server. This way, you keep personal account information out of the cookie used by the client to tell to your server clients are authenticated and keep the *key* to access the data stored on the server.

`passport@~0.4.1` and `express-session@~1.17.1` are already installed, and are both listed as dependencies in your `package.json` file.

You will need to set up the session settings and initialize Passport. First, create the variables `session` and `passport` to require `express-session` and `passport` respectively.

Then, set up your Express app to use the session by defining the following options:

```javascript
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
cookie: { secure: false }
}));
```

Be sure to add `SESSION_SECRET` to your `.env` file, and give it a random value. This is used to compute the hash used to encrypt your cookie!

After you do all that, tell your express app to **use** `passport.initialize()` and `passport.session()`.

Submit your page when you think you've got it right. If you're running into errors, you can <a href="https://forum.freecodecamp.org/t/advanced-node-and-express/567135#set-up-passport-3" target="_blank" rel="noopener noreferrer nofollow">check out the project completed up to this point</a>.

# --hints--

Passport and Express-session should be dependencies.

```js
async () => {
const url = new URL("/_api/package.json", code);
const res = await fetch(url);
const packJson = await res.json();
assert.property(
packJson.dependencies,
'passport',
'Your project should list "passport" as a dependency'
);
assert.property(
packJson.dependencies,
'express-session',
'Your project should list "express-session" as a dependency'
);
}
```

Dependencies should be correctly required.

```js
async () => {
const url = new URL("/_api/server.js", code);
const res = await fetch(url);
const data = await res.text();
assert.match(
data,
/require.*("|')passport("|')/,
'You should have required passport'
);
assert.match(
data,
/require.*("|')express-session("|')/,
'You should have required express-session'
);
}
```

Express app should use new dependencies.

```js
async () => {
const url = new URL("/_api/server.js", code);
const res = await fetch(url);
const data = await res.text();
assert.match(data, /passport\.initialize/, 'Your express app should use "passport.initialize()"');
assert.match(data, /passport\.session/, 'Your express app should use "passport.session()"');
}
```

Session and session secret should be correctly set up.

```js
async () => {
const url = new URL("/_api/server.js", code);
const res = await fetch(url);
const data = await res.text();
assert.match(
data,
/secret *:\s*process\.env(\.SESSION_SECRET|\[(?<q>"|')SESSION_SECRET\k<q>\])/,
'Your express app should have express-session set up with your secret as process.env.SESSION_SECRET'
);
}
```

Loading