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

FS JS PP01 Programming Constructs Practice Problems

The document provides a comprehensive guide on installing Node.js and Visual Studio Code, including step-by-step instructions for using Homebrew on various operating systems. It also covers Node.js features such as REPL, debugging, and programming constructs like loops and functions, along with practice problems for hands-on learning. Additionally, it includes examples of using conditional statements and case statements in JavaScript.

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 PP01 Programming Constructs Practice Problems

The document provides a comprehensive guide on installing Node.js and Visual Studio Code, including step-by-step instructions for using Homebrew on various operating systems. It also covers Node.js features such as REPL, debugging, and programming constructs like loops and functions, along with practice problems for hands-on learning. Additionally, it includes examples of using conditional statements and case statements in JavaScript.

Uploaded by

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

Javascript

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
VSCode Installation
• You can install VSCode from below link according to your OS:
https://code.visualstudio.com/
Node REPL and Debugger
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.
REPL - Read Eval Print Loop

Just like shell script 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: 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: In Chrome Browser Open chrome://inspect
Step 3: Click the Open dedicated DevTools for Node link.
Step 4: Type node inspect add.js

Step 5: Browser will open in Debug mode. Please refer next slide.
Step 6: On the debug prompt use next to proceed to next line
Step 7: cont to complete the Program execution and
Step 8: use .exit or ctrlC twice to exit node command prompt.
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

Local Variable Scope


JS Data Types
Demonstrate Node
Programming Constructs
Sequence Practice Problems

1. Use REPL – Random Function Math.floor(Math.random() * 10); to get Single


Digit.
2. Use REPL – Use Random to get Dice Number between 1 to 6
3. Use REPL – Add two Random Dice Number and Print the Result
4. Use Script & Debug – Write a program that reads 5 Random 2 Digit values ,
then find their sum and the average
5. Use Script & Debug – Unit Conversion
a. 1ft = 12 in then 42 in = ? ft
b. Rectangular Plot of 60 feet x 40 feet in meters
c. Calculate area of 25 such plots in acres
If Statements
Selection Practice Problems with if & else

1. Write a program that reads 5 Random 3 Digit values and then outputs the minimum
and the maximum value
2. Write a program that takes day and month from the command line and prints true if
day of month is between March 20 and June 20, false otherwise.
3. Write a program that takes a year as input and outputs the Year is a Leap Year or not
a Leap Year. A Leap Year checks for 4 Digit Number, Divisible by 4 and not 100 unless
divisible by 400.
4. Write a program to simulate a coin flip and print out "Heads" or "Tails" accordingly.
Selection Practice Problems with if else if and else

1. Read a single digit number and write the number in word


2. Read a Number and Display the week day (Sunday, Monday,…)
3. Read a Number 1, 10, 100, 1000, etc and display unit, ten, hundred,…
4. Enter 3 Numbers do following arithmetic operation and find the one that
is maximum and minimum
1. a + b * c 3. c + a / b
2. a % b + c 4. a * b + c
Case Statements
Selection Practice Problems with case statements

1. Read a single digit number and write the number in word using Case
2. Read a Number and Display the week day (Sunday, Monday,…)
3. Read a Number 1, 10, 100, 1000, etc and display unit, ten, hundred,…
4. Write a program that takes User Inputs and does Unit Conversion of
different Length units
1. Feet to Inch 3. Inch to Feet
2. Feet to Meter 4. Meter to Feet
for Loop Statement
Repetition Practice Problems with for loop
1. Write a program that takes a command-line argument n and prints a table of the
powers of 2 that are less than or equal to 2^n.
2. Write a program that takes a command-line argument n and prints the nth harmonic
number. Harmonic Number is of the form

3. Write a program that takes a input and determines if the number is a prime.
4. Extend the program to take a range of number as input and output the Prime
Numbers in that range.
5. Write a program that computes a factorial of a number taken as input.
5 Factorial – 5! = 1 * 2 * 3 * 4 * 5
6. Write a program to compute Factors of a number N using prime factorization method.
Logic -> Traverse till i*i <= N instead of i <= N for efficiency.
O/P -> Print the prime factors of number N.
while Loop Statement
Repetition Practice Problems with while loop
1. Write a program that takes a command-line argument n and prints a
table of the powers of 2 that are less than or equal to 2^n till 256 is
reached..
2. Find the Magic Number
a. Ask the user to think of a number n between 1 to 100
b. Then check with the user if the number is less then n/2 or greater
c. Repeat till the Magic Number is reached..
3. Extend the Flip Coin problem till either Heads or Tails wins 11 times.
4. Write a Program where a gambler starts with Rs 100 and places Re 1 bet
until he/she goes broke i.e. no more money to gamble or reaches the
goal of Rs 200. Keeps track of number of times won and number of bets
made.
• 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
4. Functions particularly useful if you have certain
tasks which need to be performed
several times.
Demonstrate
Functions and
Functional
Expressions
Functions Practice Problems
1. Help user find degF or degC based on their Conversion Selection. Use
Case Statement and ensure that the inputs are within the Freezing Point (
0 °C / 32 °F ) and the Boiling Point of Water ( 100 °C / 212 °F )
a. degF = (degC * 9/5) + 32
b. degC = (degF – 32) * 5/9
2. Write a function to check if the two numbers are Palindromes
3. Take a number from user and check if the number is a Prime then show
that its palindrome is also prime
a. Write function check if number is Prime
b. Write function to get the Palindrome.
c. Check if the Palindrome number is also prime
Thank
You

You might also like