0% found this document useful (0 votes)
4 views

FS JS DOC01 Programming Constructs with NodeJS

The document provides a comprehensive guide on installing Node.js and npm using Homebrew on various operating systems, along with instructions for setting up VSCode. It also covers the basics of Node.js, including its REPL environment, debugging tools, and programming constructs such as sequences, selection statements, functions, and repetition. Additionally, it includes examples for calculating employee wages based on different working conditions.

Uploaded by

rarite5601
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

FS JS DOC01 Programming Constructs with NodeJS

The document provides a comprehensive guide on installing Node.js and npm using Homebrew on various operating systems, along with instructions for setting up VSCode. It also covers the basics of Node.js, including its REPL environment, debugging tools, and programming constructs such as sequences, selection statements, functions, and repetition. Additionally, it includes examples for calculating employee wages based on different working conditions.

Uploaded by

rarite5601
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

NodeJs

Programming
Constructs
NodeJs – CLI (Command Line Interface) Installation
• Step 1: Check Homebrew Package Manager is installed using
• brew –version - in the Terminal of Linux/Mac or in the Command Line of Windows

• Step 2: If not, Installation of Homebrew Package Manager


• For Windows – Download Windows Subsystem for Linus using link https://docs.microsoft.com/en-
us/windows/wsl/about
• For Linux & Windows - https://docs.brew.sh/Homebrew-on-Linux
• For Mac - https://docs.brew.sh/Installation
• Step 3: brew –version in command line to check for successful installation
• Step 4: brew –update – This updates Homebrew with a list of the latest version of Node.
• Step 5: brew install node – This will install node and npm
• Step 6: node -v & npm -v – This will check the installation of node and npm
• Refer https://treehouse.github.io/installation-guides/mac/node-mac.html for installation notes
for Mac, Linux and Windows with Homebrew
1. Node.js® is an environment
which you can uses for compiling
and running JavaScript code in
command line and more
What is Node.js importantly to create web-
and npm servers and networked
applications.
2. NPM is a “package manager”
that makes installing Node
“packages” fast and easy.
VSCode Installation
• You can install VSCode from below link according to your OS:
https://code.visualstudio.com/
Demonstrate Node REPL
and Debugger
REPL - Read Eval Print Loop
Just like shell script/MySQL Client can be run in Command
Line, Node comes with bundled REPL Environment
Step 1: Type node in your terminal
Step 2: In command prompt type
console.log(“Hello”)
Step 3: Use .exit or ctrlC twice to exit node
command prompt.
Step 4: Random Function for single digit
Math.floor(Math.random() * 10);.
Step 4: Click to Practice.
Node.js Debugger

Step 1: Type nano add.js and write code


let – Variable Declaration Local
Scope. New Way
var – Variable Declaration Global
Scope. Old Way

Step 2: The latest debug version can also be installed


independently (e.g. npm install -g node-inspect)
Step 3: In Chrome Browser Open chrome://inspect
Step 4: Click the Open dedicated DevTools for Node link.
Step 5: Type node inspect add.js
Step 6: Open Chrome and In URL Type chrome://inspect/#devices
Click Here
Node.js
Debugger Click On Inspect
Node.js Debugger Window
add.js debug console
F8 – Resume Script Execution
F9 – Execute the Current Line
F10 – Step Over
F11 – Step Into
Shift F11 – Step Out

Step 5: Browser will open in


Debug mode.
Step 6: On the debug prompt
use next to proceed to next
line
Local Variable Scope
Step 7: cont to complete the
Program execution and
Step 8: use .exit or ctrlC twice
to exit node command
prompt.
JS Data Types
Demonstrate Node
Programming Constructs
1. Sequences
2. Selection
3. Functions
Programming 4. Repetition
Constructs 5. Arrays
Classification 6. Map
7. Patterns
Note: js cheatsheet
1. Sequence Statement

A sequence construct tells the CPU (processor)


which statement is to be executed next.
Step 1: Create NodeDevelopment Directory using your terminal
Step 2: In Node Development Directory create hello.js file using nano –T 3 hello.js
Step 3: Execute using node hello.js
Step 4: Print hello using console.log function

Use console.log function to print to the


terminal
1. A selection statement
provides for selection
between alternatives
2. Selection 2. A program can take certain
Statement route depending on a
situation and selection
statements help in choosing
between the routes.
2. Selection 1. If statements
Statement 2. Case Statements
Types 3. Pattern Matching
If Statements
Ability to Check Employee is
present or Absent
- Use Math.Random to check Absent or
Present
- Use const which signal that the identifier
won't be reassigned. And use let where the
variable may be reassigned. Both have Block

UC 1 Scope. Traditional var has function scope


Check Employee Presence UC 1
Ability to Calculate Daily
Employee Wage based on
part time or full time work
- Use Math.Random to check No Time,
Part Time or Full Time
- Assume Part Time is 4 Hrs and Full time
is 8 Hrs and per hour wage is $20
UC 2 - Solve Using Switch Statement
Calculating Employee Wage UC 2
Calculating Employee Wage UC 2
• Functions are a great way to reuse
code.
• Think of a function as a small script
within a script. It's a small chunk of
code which you may call multiple
times within your script. They are
3. Functions particularly useful if you have certain
tasks which need to be performed
several times.
Demonstrate
Functions and
Functional
Expressions
Refactor the Code
to write a function
to get work hours

UC 3
Refactored code to write function for daily working Hours
A repetition construct causes
a group of one or more
4. Repetition
program statements to be
Statement
invoked repeatedly until
some end condition is met.
1. Fixed count loops - repeat a
predefine number of times.
4. Repetition for ( … ) { … }
Statement 2. Variable count loops - repeat
Types an unspecified number of
times.
while ( … ) { … }
for Loop Statement
Calculating Wages for
a Month assuming 20
Working Days in a
Month
UC 4
Calculating Wages for a Month
while Loop Statement
Calculate Wages till a
condition of total
working hours of 160 or
max days of 20 is
reached for a month
UC 5
Calculating Wages till Number of Working Days or Total Working
Hours per month is Reached
Thank
You

You might also like