You are on page 1of 23

JavaScript

Introduction To JavaScript
JavaScript Functions
Functions are a set of organized instructions used to carry out a task.
Function Features:
• A list of arguments which can be anything and is
optional. This is the input to the function.
• A program block is executed on the invocation of the
function.
• An output that is returned after the function finishes
executing using a return statement.

Introduction:
Introduction:
JavaScript Functions:
We will cover:
• Nested Functions
• Arrow Functions
• Function as an Argument
• Function as a Returned Object

Array Methods:
Some methods can be invoked to execute the function:
• forEach
• Filter
• Find
• findIndex

Exercise
Creating Boards
Task:
An ambitious game developer wants to make one of the top 10 games
in the world. However, there is a problem. Because he makes board
games, it takes a lot of his time to create the board and populate it with
game pieces. To speed up this process, he has asked you to make his
work quicker and simpler.
Your task is to create a function that first generates a board (two-
dimensional array) and then populates that board with values using an
array of pieces, where each piece is an object with board coordinates
and the value of the piece within the board.

Illustration:
Problem Statement:
The game designer gives you three arguments: length, width, and pieces.
Each argument is as follows:
• length (First argument): This is the length of the outer array of the two-dimensional array representation of the board.
• width (Second argument): This is the length of the inner arrays of the two-dimensional array representation of the
board.
• pieces (Third argument): This is an array of objects where each object has the following properties:
◦ row: Has a number value representing the index of the outer array of two-dimensional array representations of
the board.
◦ col: Has a number value representing the index of the inner array of two-dimensional array representations of
the board.
◦ value: Is the string value representing the piece that will be assigned to the respective index inside the two-
dimensional array representation of the board.

Create a two-dimensional array of the respective length and width and populate it with the pieces’ values of the objects
residing in the array, passed to the third (pieces) argument. All unpopulated indices will be assigned to " " value (a string
with only a single space character).

JavaScript Scope
The scope is the extent to which a variable is accessible throughout the program at run-time.
Introduction:
Scope Features:
• It limits the visibility and accessibility of a variable.
• It controls the life of the variable and
the assigned resources.
• It helps secure resources and makes debugging easier

Scopes In JS:
JavaScript has two scopes:
• Global
• Local
• Function Scope
• Block Scope

Lexical Scope:
Module Scope:
Exercise
Debugging 101
Task:
The mathematician needs help. He wrote some code to calculate the
square of two numbers. However, he wrote lots of unnecessary
statements. His math and logic are correct. He correctly used loops
and the addition statement to find the square while also using a
function to sum two numbers together. But he doesn’t know how to
satisfy his conditionals so that the code works smoothly. Fix his code
such that only certain values are changed, not the entire code. We
would not want to hurt the mathematician’s feelings by totally rewriting
his code.

Problem Statement:
You have a function assigned to square which calculates the
square of the number passed as argument n. The following
lines need to be updated.
• Line 9: Replace NaN with an appropriate number that satisfies the conditional.
• Line 15: Replace NaN with an appropriate number that satisfies the
conditional.
• Line 20: Replace the two NaN values with the appropriate numbers that satisfy
the conditional.
• Line 25: Replace NaN with the variable that will be returned to give the correct
answer.

Reminder, rules so far


1. Think before you program!
2. A program is a human-readable essay on problem solving that also happens to
execute on a computer.
3. The best way to improve your programming and problem-solving skills is to
practice!
4. A foolish consistency is the hobgoblin of little minds
5. Test your code, often and thoroughly
6. If it was hard to write, it is probably hard to read. Add a comment.
7. All input is evil, unless proven otherwise.
8. A function should do one thing.

You might also like