Homework & Coding Practice

Homework/Practice Coding Assignment (20-min)

Part 1: Set Up a Simple Node.js Project Directory Structure

Steps:

  1. Create a new directory:

    bash코드 복사mkdir my-node-project
    cd my-node-project
  2. Initialize a new package.json file:

    bash코드 복사npm init -y
  3. Add a script to package.json:

    json코드 복사{
      "name": "my-node-project",
      "version": "1.0.0",
      "scripts": {
        "start": "node index.js"
      }
    }
  4. Create an index.js file:

    javascript코드 복사console.log('Welcome to My Node.js Project');
  5. Run the project:

    bash코드 복사npm start

Part 2: Write and Save a Short Script Using REPL

Steps:

  1. Open the REPL:

    bash코드 복사node
  2. Write and test the following:

    javascript코드 복사// Define a multiplication function
    function multiply(a, b) {
      return a * b;
    }
    
    // Call the function
    multiply(3, 7);
  3. Save the commands to a file script.js:

    javascript코드 복사const multiply = (a, b) => a * b;
    console.log('3 x 7 =', multiply(3, 7));
  4. Exit the REPL and run the script:

    bash코드 복사node script.js
  5. Verify the output.

Expected Outcomes

  1. Students successfully set up a Node.js environment and understand how to manage versions with nvm.

  2. They gain hands-on experience with the REPL and basic scripting.

  3. Homework reinforces project initialization and debugging concepts.

Last updated