Review all the lab workbooks we worked through in class.
Here's a detailed lab learning guide for students to develop their JavaScript skills using Node.js and Visual Studio Code:
---
# Lab Guide: Fundamental JavaScript Programming with Node.js
## Objective
By the end of this lab, you will be able to write simple JavaScript programs demonstrating basic programming concepts including variable assignments, comparisons, decision-making structures, loops, and functions.
1. Install Node.js from the official website: https://nodejs.org/
2. Install Visual Studio Code from: https://code.visualstudio.com/
3. Open Visual Studio Code and create a new folder for your JavaScript exercises.
4. In this folder, create a new file called `fundamentals.js`.
## Part 2: Variable Assignments and Comparisons
In `fundamentals.js`, add the following code:
```javascript
// Variable assignments
let x = 10;
const y = 20;
var z = 30;
console.log("x =", x, "y =", y, "z =", z);
// Comparisons
console.log("x < y:", x < y);
console.log("y > z:", y > z);
console.log("x === z:", x === z);
console.log("x !== y:", x !== y);
```
Run this code by opening a terminal in VS Code (Terminal -> New Terminal) and typing:
```
node fundamentals.js
```
Observe the output and make sure you understand each comparison.
## Part 3: Decision Making
Add the following code to your `fundamentals.js` file:
```javascript
// If-then statement
let age = 18;
if (age >= 18) {
console.log("You are an adult");
} else {
console.log("You are a minor");
}
// Switch statement
let day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the work week");
break;
case "Friday":
console.log("End of the work week");
break;
default:
console.log("It's a regular day");
}
```
Run the code again and observe the output. Try changing the `age` and `day` variables to see different results.
## Part 4: Loops
Add these loops to your file:
```javascript
// For loop
console.log("Counting from 1 to 5:");
for (let i = 1; i <= 5; i++) {
console.log(i);
}
// While loop
console.log("Counting down from 5 to 1:");
let count = 5;
while (count > 0) {
console.log(count);
count--;
}
```
Run the code and observe how each loop behaves.
## Part 5: Functions
Add this function to your file:
```javascript
// Function to calculate the square of a number
function square(number) {
return number * number;
}
console.log("The square of 7 is:", square(7));
// Arrow function to calculate the cube of a number
const cube = (number) => number * number * number;
console.log("The cube of 3 is:", cube(3));
```
Run the code and observe how functions can be defined and used.
## Part 6: Putting It All Together
Now, let's write a program that adds the numbers from 1 to 100, stepping up by 7 each time and summing all the numbers stepped over. Add this to your file:
```javascript
function sumWithStep(max, step) {
let sum = 0;
for (let i = 1; i <= max; i += step) {
sum += i;
}
return sum;
}
const max = 100;
const step = 7;
const result = sumWithStep(max, step);
console.log(`The sum of numbers from 1 to ${max}, stepping by ${step}, is: ${result}`);
```
Run this code and verify the result.
## Challenges
1. Modify the `sumWithStep` function to also return the count of numbers that were summed.
2. Create a new function that finds all prime numbers up to a given maximum number.
3. Write a program that uses a switch statement to perform different arithmetic operations (addition, subtraction, multiplication, division) based on user input.
## Conclusion
In this lab, you've practiced fundamental JavaScript concepts including variable assignments, comparisons, decision-making structures, loops, and functions. You've also written a program that performs a specific summing task. Remember to experiment with the code, try the challenges, and don't hesitate to explore further!
---
This lab guide provides a structured approach to learning and practicing fundamental JavaScript concepts using Node.js and Visual Studio Code. It covers all the requested topics and includes a specific exercise for summing numbers with a step, as well as additional challenges for further practice.
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (