SlideShare a Scribd company logo
Course Code: CSE 202
Course Title: Computer Programming Lab
Lecture 6: Array in JS
Course Teacher: Saurav Barua (SB)
Assistant Professor, Dept. of CE, DIU
Phone: +88-01715334075
Email: saurav.ce@diu.edu.bd
Contents
2
Array methods
Creating and
accessing array
elements
Array index
Modify array
elements
JavaScript
arrays
.
2D array
JavaScript arrays
3
 An array in JavaScript is a data structure used to store multiple
values in a single variable.
 It can hold various data types and allows for dynamic resizing.
 Elements are accessed by their index, starting from 0.
Creating and accessing array elements
4
// Creating an Array and Initializing with Values
let a = ["HTML", "CSS", "JS"];
// Accessing Array Elements
console.log(a[0]); //output: HTML
Array index
5
Example:
const array = [2, 16, 24, 32, 4];
Modify array elements
6
 Elements in an array can be modified by assigning a new
value to their corresponding index.
Example:
// Creating an Array and Initializing with Values
let a = ["HTML", "CSS", "JS"];
console.log(a); //output: [ 'HTML', 'CSS', 'JS' ]
a[1]= "Bootstrap";
console.log(a); //output: [ 'HTML', 'Bootstrap', 'JS' ]
Array methods
7
Methods Description Example
push() add the element to the end
of the array.
let a = [2, 4, 6]
a.push(3);
console.log(a); //output: 2, 4, 6, 3
unshift() add the element to the
starting of the array.
let a = [2, 4, 6]
a.unshift(5);
console.log(a); //output: 5, 2, 4, 6
pop() removes an element from
the last index of the array
let a = [2, 4, 6]
a.pop();
console.log(a); //output: 2, 4
Array methods
8
Methods Description Example
shift() removes the element from the
first index of the array.
let a = [2, 4, 6]
a.shift();
console.log(a); //output: 4, 6
splice() removes or replaces the
element from the array
let a = [2, 4, 6, 7]
a.splice(1,2);
console.log(a); //output: 4, 6
Two dimensional array
9
 A two-dimensional array, also known as a 2D array, is a collection of data
elements arranged in a grid-like structure with rows and columns.
Example:
let MathScore = [
['John Doe', 20, 60, 'A'],
['Jane Doe', 10, 52, 'B'],
['Petr Chess', 5, 24, 'F'],
['Ling Jess', 28, 43, 'A'],
['Ben Liard', 16, 51, 'B']
];
Access Elements
arrayName[rowIndex][columnIndex]
console.log(MathScore[4][0]); // returns 'Ben Liard'
console.log(MathScore[2][1]); // returns 5
Worked out example
10
Example 6: Program to find the maximum value from a 2D array and display the
name in browser. (Find the month of highest rainfall from a given rainfall intensity
(mm) data and display the month in the browser.)
Google drive link:
https://drive.google.com/drive/folders/1VqaAUKCxAYzXJPxeddnOKUR644a
rhJNs?usp=drive_link
Git-hub link: https://github.com/sauravbarua02/rainfallIntensity2DArray
Interface
11
html codes
12
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-
scale=1.0">
<title>Rainfall intensity</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container" id="container">
</div>
<script src="app.js"></script>
</body>
</html>
css codes
13
body{
margin: 0px;
background-color: rgba(210,130,130,0.5);
}
.container{
background-color:
rgba(210,130,130,0.3);
height: 300px;
width: 300px;
margin: 20px auto;
padding: 10px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: larger;
box-shadow: 0 0 4px 1px rgba(0,0,0,0.6);
border-radius: 10px;
}
JS codes
14
const containerEl =
document.getElementById("container");
let rainfallInfo = [["January",
25],["February", 50], ["March", 10],
["July", 70]];
function maxRainfall(rainfallInfo){
let maxRainfallMonth =
rainfallInfo[0][0];
let maxRainfallIntensity =
rainfallInfo[0][1];
for(let i=1; i<rainfallInfo.length;
i++){
if(maxRainfallIntensity <
rainfallInfo[i][1]){
maxRainfallIntensity =
rainfallInfo[i][1];
maxRainfallMonth =
rainfallInfo[i][0];
}
}
return maxRainfallMonth;
}
function display(){
let output = maxRainfall(rainfallInfo);
containerEl.innerText = `The highest
rainfall in ${output} month`;
}
display();
Class tasks
15
Task 6.1: Program to find the governing capacity (kip) of a steel plate from the shear, tension
and bearing capacity data and display which capacity governs in the browser. (Hints: use “>” sign
in the logic for the minimum value and governing capacity is the lowest/minimum capacity.)
Task 6.2: Program to find the governing bending moment (kft/ft) on a concrete column from
the earthquake X-direction, earthquake X-direction, wind X-direction and wind Y-direction
moment data and display which moment governs in the browser. (Hints: maximum moment is the
governing)
Task 6.3: Program to find maximum traffic flow (veh/hr) from a 5-hrs traffic count and display
which hour has the maximum traffic flow. (Hints: designate hour as 7AM, 8PM.. etc.)
Task 6.4: Program to find which contractor wins a contract in the tender bidding for a bridge
project and display which the name of the contractor in the browser. (Hints: A contractor who
submits the lowest price quotation in BDT will win the contract. use “>” sign in the logic for the minimum
value)
Task 6.5: Program to find the highest sludge discharge (ml/g) rate in a sewage canal from the
4-day data for a City and display which day has the highest sludge discharge in the browser.
(hints: days will be Sunday, Monday.. etc.)
End of the Lecture
16

More Related Content

Similar to L6, Array in JS, CSE 202, BN11.pdf JavaScript (20)

PPTX
Lecture 9_Classes.pptx
NelyJay
 
PPTX
Data visualization in python/Django
kenluck2001
 
PDF
CS Sample Paper 1
kvs
 
PPTX
12Structures.pptx
Aneeskhan326131
 
PDF
Introduction to R Programming
izahn
 
PDF
CP Handout#9
trupti1976
 
PPTX
Unit 3
GOWSIKRAJAP
 
PPTX
Array BPK 2
Riki Afriansyah
 
PDF
Intake 38 3
Mahmoud Ouf
 
PDF
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 5.pdf
amanpathak160605
 
DOC
Sp 1418794917
lakshmi r
 
PDF
Boost delivery stream with code discipline engineering
Miro Wengner
 
PPT
Chapter 6 arrays part-1
Synapseindiappsdevelopment
 
PDF
C# p9
Renas Rekany
 
PDF
Data manipulation on r
Abhik Seal
 
PPT
Csphtp1 07
HUST
 
PPTX
Unit 3
GOWSIKRAJAP
 
PDF
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
Supriya Radhakrishna
 
PDF
Algorithms devised for a google interview
Russell Childs
 
PPT
javascript arrays in details for udergaduate studenets .ppt
debasisdas225831
 
Lecture 9_Classes.pptx
NelyJay
 
Data visualization in python/Django
kenluck2001
 
CS Sample Paper 1
kvs
 
12Structures.pptx
Aneeskhan326131
 
Introduction to R Programming
izahn
 
CP Handout#9
trupti1976
 
Unit 3
GOWSIKRAJAP
 
Array BPK 2
Riki Afriansyah
 
Intake 38 3
Mahmoud Ouf
 
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 5.pdf
amanpathak160605
 
Sp 1418794917
lakshmi r
 
Boost delivery stream with code discipline engineering
Miro Wengner
 
Chapter 6 arrays part-1
Synapseindiappsdevelopment
 
Data manipulation on r
Abhik Seal
 
Csphtp1 07
HUST
 
Unit 3
GOWSIKRAJAP
 
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
Supriya Radhakrishna
 
Algorithms devised for a google interview
Russell Childs
 
javascript arrays in details for udergaduate studenets .ppt
debasisdas225831
 

More from SauravBarua11 (20)

PDF
L11, Project Survey , Spring 24, lecture notes, SB.pdf
SauravBarua11
 
PDF
L5, Computation of Area, Spring 24, SB.pdf
SauravBarua11
 
PDF
L4, Contour, Spring 24, lecture notes, SB.pdf
SauravBarua11
 
PDF
L3, Traverse Survey, Spring 24, lecture notes, SB.pdf
SauravBarua11
 
PDF
L8, Tacheometry survey, Spring 24, SB.pdf
SauravBarua11
 
PDF
L10, Astronomical surveying, Spring 24, SB.pdf
SauravBarua11
 
PDF
L6, Computation of Volume, Spring 24, SB.pdf
SauravBarua11
 
PDF
L9, photogrammetric survey, Spring 24, SB.pdf
SauravBarua11
 
PDF
L2, Level surveying, Spring 24,class notes, SB.pdf
SauravBarua11
 
PDF
Confusion matrix in Transportation Engineering.pdf
SauravBarua11
 
PDF
Ordinary least square (OLS) and MLE in Transportation Engineering.pdf
SauravBarua11
 
PDF
L5, Loop and iteration, CSE 202, BN11.pdf
SauravBarua11
 
PDF
L2. Function in JS, CSE 202, BN11.p1df documents
SauravBarua11
 
PDF
L4, Conditional statement, CSE 202 JavaScript
SauravBarua11
 
PDF
L3. Operators in JS, CSE 202, BN11.pdf JavaScript
SauravBarua11
 
PDF
L1. Introduction, CSE 202, BN11.pdf JavaScript
SauravBarua11
 
PDF
L7. Object in JS, CSE 202, BN11.pdf JavaScript
SauravBarua11
 
PDF
L10. Math.random method in JS, CSE 202, BN11.pdf
SauravBarua11
 
PDF
L8. Constructor and method in JS, CSE 202, BN11.pdf
SauravBarua11
 
PDF
L11, Switch, break and continue statements, CSE 202, BN11.pdf
SauravBarua11
 
L11, Project Survey , Spring 24, lecture notes, SB.pdf
SauravBarua11
 
L5, Computation of Area, Spring 24, SB.pdf
SauravBarua11
 
L4, Contour, Spring 24, lecture notes, SB.pdf
SauravBarua11
 
L3, Traverse Survey, Spring 24, lecture notes, SB.pdf
SauravBarua11
 
L8, Tacheometry survey, Spring 24, SB.pdf
SauravBarua11
 
L10, Astronomical surveying, Spring 24, SB.pdf
SauravBarua11
 
L6, Computation of Volume, Spring 24, SB.pdf
SauravBarua11
 
L9, photogrammetric survey, Spring 24, SB.pdf
SauravBarua11
 
L2, Level surveying, Spring 24,class notes, SB.pdf
SauravBarua11
 
Confusion matrix in Transportation Engineering.pdf
SauravBarua11
 
Ordinary least square (OLS) and MLE in Transportation Engineering.pdf
SauravBarua11
 
L5, Loop and iteration, CSE 202, BN11.pdf
SauravBarua11
 
L2. Function in JS, CSE 202, BN11.p1df documents
SauravBarua11
 
L4, Conditional statement, CSE 202 JavaScript
SauravBarua11
 
L3. Operators in JS, CSE 202, BN11.pdf JavaScript
SauravBarua11
 
L1. Introduction, CSE 202, BN11.pdf JavaScript
SauravBarua11
 
L7. Object in JS, CSE 202, BN11.pdf JavaScript
SauravBarua11
 
L10. Math.random method in JS, CSE 202, BN11.pdf
SauravBarua11
 
L8. Constructor and method in JS, CSE 202, BN11.pdf
SauravBarua11
 
L11, Switch, break and continue statements, CSE 202, BN11.pdf
SauravBarua11
 
Ad

Recently uploaded (20)

PPTX
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
PPTX
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
PDF
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
PDF
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
PPTX
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
 
PPTX
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
 
PPTX
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
 
PDF
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
PPTX
Computer network Computer network Computer network Computer network
Shrikant317689
 
PPTX
Functions in Python Programming Language
BeulahS2
 
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
 
PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PPTX
WHO And BIS std- for water quality .pptx
dhanashree78
 
PDF
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
PPTX
Work at Height training for workers .pptx
cecos12
 
PDF
01-introduction to the ProcessDesign.pdf
StiveBrack
 
PDF
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
 
PDF
PRIZ Academy - Process functional modelling
PRIZ Guru
 
PDF
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
PPTX
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
 
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
 
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
 
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
Computer network Computer network Computer network Computer network
Shrikant317689
 
Functions in Python Programming Language
BeulahS2
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
WHO And BIS std- for water quality .pptx
dhanashree78
 
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
Work at Height training for workers .pptx
cecos12
 
01-introduction to the ProcessDesign.pdf
StiveBrack
 
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
 
PRIZ Academy - Process functional modelling
PRIZ Guru
 
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
Ad

L6, Array in JS, CSE 202, BN11.pdf JavaScript

  • 1. Course Code: CSE 202 Course Title: Computer Programming Lab Lecture 6: Array in JS Course Teacher: Saurav Barua (SB) Assistant Professor, Dept. of CE, DIU Phone: +88-01715334075 Email: [email protected]
  • 2. Contents 2 Array methods Creating and accessing array elements Array index Modify array elements JavaScript arrays . 2D array
  • 3. JavaScript arrays 3  An array in JavaScript is a data structure used to store multiple values in a single variable.  It can hold various data types and allows for dynamic resizing.  Elements are accessed by their index, starting from 0.
  • 4. Creating and accessing array elements 4 // Creating an Array and Initializing with Values let a = ["HTML", "CSS", "JS"]; // Accessing Array Elements console.log(a[0]); //output: HTML
  • 5. Array index 5 Example: const array = [2, 16, 24, 32, 4];
  • 6. Modify array elements 6  Elements in an array can be modified by assigning a new value to their corresponding index. Example: // Creating an Array and Initializing with Values let a = ["HTML", "CSS", "JS"]; console.log(a); //output: [ 'HTML', 'CSS', 'JS' ] a[1]= "Bootstrap"; console.log(a); //output: [ 'HTML', 'Bootstrap', 'JS' ]
  • 7. Array methods 7 Methods Description Example push() add the element to the end of the array. let a = [2, 4, 6] a.push(3); console.log(a); //output: 2, 4, 6, 3 unshift() add the element to the starting of the array. let a = [2, 4, 6] a.unshift(5); console.log(a); //output: 5, 2, 4, 6 pop() removes an element from the last index of the array let a = [2, 4, 6] a.pop(); console.log(a); //output: 2, 4
  • 8. Array methods 8 Methods Description Example shift() removes the element from the first index of the array. let a = [2, 4, 6] a.shift(); console.log(a); //output: 4, 6 splice() removes or replaces the element from the array let a = [2, 4, 6, 7] a.splice(1,2); console.log(a); //output: 4, 6
  • 9. Two dimensional array 9  A two-dimensional array, also known as a 2D array, is a collection of data elements arranged in a grid-like structure with rows and columns. Example: let MathScore = [ ['John Doe', 20, 60, 'A'], ['Jane Doe', 10, 52, 'B'], ['Petr Chess', 5, 24, 'F'], ['Ling Jess', 28, 43, 'A'], ['Ben Liard', 16, 51, 'B'] ]; Access Elements arrayName[rowIndex][columnIndex] console.log(MathScore[4][0]); // returns 'Ben Liard' console.log(MathScore[2][1]); // returns 5
  • 10. Worked out example 10 Example 6: Program to find the maximum value from a 2D array and display the name in browser. (Find the month of highest rainfall from a given rainfall intensity (mm) data and display the month in the browser.) Google drive link: https://drive.google.com/drive/folders/1VqaAUKCxAYzXJPxeddnOKUR644a rhJNs?usp=drive_link Git-hub link: https://github.com/sauravbarua02/rainfallIntensity2DArray
  • 12. html codes 12 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial- scale=1.0"> <title>Rainfall intensity</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container" id="container"> </div> <script src="app.js"></script> </body> </html>
  • 13. css codes 13 body{ margin: 0px; background-color: rgba(210,130,130,0.5); } .container{ background-color: rgba(210,130,130,0.3); height: 300px; width: 300px; margin: 20px auto; padding: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center; font-size: larger; box-shadow: 0 0 4px 1px rgba(0,0,0,0.6); border-radius: 10px; }
  • 14. JS codes 14 const containerEl = document.getElementById("container"); let rainfallInfo = [["January", 25],["February", 50], ["March", 10], ["July", 70]]; function maxRainfall(rainfallInfo){ let maxRainfallMonth = rainfallInfo[0][0]; let maxRainfallIntensity = rainfallInfo[0][1]; for(let i=1; i<rainfallInfo.length; i++){ if(maxRainfallIntensity < rainfallInfo[i][1]){ maxRainfallIntensity = rainfallInfo[i][1]; maxRainfallMonth = rainfallInfo[i][0]; } } return maxRainfallMonth; } function display(){ let output = maxRainfall(rainfallInfo); containerEl.innerText = `The highest rainfall in ${output} month`; } display();
  • 15. Class tasks 15 Task 6.1: Program to find the governing capacity (kip) of a steel plate from the shear, tension and bearing capacity data and display which capacity governs in the browser. (Hints: use “>” sign in the logic for the minimum value and governing capacity is the lowest/minimum capacity.) Task 6.2: Program to find the governing bending moment (kft/ft) on a concrete column from the earthquake X-direction, earthquake X-direction, wind X-direction and wind Y-direction moment data and display which moment governs in the browser. (Hints: maximum moment is the governing) Task 6.3: Program to find maximum traffic flow (veh/hr) from a 5-hrs traffic count and display which hour has the maximum traffic flow. (Hints: designate hour as 7AM, 8PM.. etc.) Task 6.4: Program to find which contractor wins a contract in the tender bidding for a bridge project and display which the name of the contractor in the browser. (Hints: A contractor who submits the lowest price quotation in BDT will win the contract. use “>” sign in the logic for the minimum value) Task 6.5: Program to find the highest sludge discharge (ml/g) rate in a sewage canal from the 4-day data for a City and display which day has the highest sludge discharge in the browser. (hints: days will be Sunday, Monday.. etc.)
  • 16. End of the Lecture 16