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

PPT
Arrays
SARITHA REDDY
 
PPSX
javascript-Array.ppsx
VedantSaraf9
 
DOCX
What are arrays in java script
Miguel Silva Loureiro
 
PPT
Array Presentation
Deep Prajapati Microplacer
 
PPTX
17-Arrays-And-Files-kkkkkkkkkkkkkkk.pptx
kamalsmail1
 
PPT
Temporal PPT details about the platform and its uses
AliasD8
 
PPTX
JavaScript Arrays and its types .pptx
Ramakrishna Reddy Bijjam
 
PPTX
Understanding of Arrays and its types along with implementation
VISHALYADAV809458
 
javascript-Array.ppsx
VedantSaraf9
 
What are arrays in java script
Miguel Silva Loureiro
 
Array Presentation
Deep Prajapati Microplacer
 
17-Arrays-And-Files-kkkkkkkkkkkkkkk.pptx
kamalsmail1
 
Temporal PPT details about the platform and its uses
AliasD8
 
JavaScript Arrays and its types .pptx
Ramakrishna Reddy Bijjam
 
Understanding of Arrays and its types along with implementation
VISHALYADAV809458
 

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

PDF
Lecture 6 - Arrays
Syed Afaq Shah MACS CP
 
PPTX
PPT Lecture 2.2.1 onn c++ data structures
midtushar
 
PPT
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
PPTX
Data Structures - Array presentation .pptx
IshanKapoor26
 
PPT
Lecture 2a arrays
Victor Palmar
 
PDF
Array
Scott Donald
 
PDF
OpenRepGrid – An Open Source Software for the Analysis of Repertory Grids
Mark Heckmann
 
PDF
Whats new in ES2019
chayanikaa
 
PDF
Data Structures And Algorithms Roadmap for Beginners By ScholarHat PDF
Scholarhat
 
PPTX
Learn java script
Mahmoud Asadi
 
PDF
JavaScript and jQuery - Web Technologies (1019888BNR)
Beat Signer
 
PPTX
introduction of Data strutter and algirithm.pptx
ssuser7b3003
 
PDF
JavaScript in 2016
Codemotion
 
PPTX
JavaScript in 2016 (Codemotion Rome)
Eduard Tomàs
 
PPT
Data Structures: A Foundation for Efficient Programming
MSridhar18
 
PDF
GeoGebra JavaScript CheatSheet
Jose Perez
 
PDF
CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version...
CodeFest
 
PPT
Chapter 10.ppt
MithuBose3
 
PPTX
Module 2 Javascript. Advanced concepts of javascript
BKReddy3
 
PPTX
07+08slide.pptx
MURADSANJOUM
 
Lecture 6 - Arrays
Syed Afaq Shah MACS CP
 
PPT Lecture 2.2.1 onn c++ data structures
midtushar
 
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
Data Structures - Array presentation .pptx
IshanKapoor26
 
Lecture 2a arrays
Victor Palmar
 
OpenRepGrid – An Open Source Software for the Analysis of Repertory Grids
Mark Heckmann
 
Whats new in ES2019
chayanikaa
 
Data Structures And Algorithms Roadmap for Beginners By ScholarHat PDF
Scholarhat
 
Learn java script
Mahmoud Asadi
 
JavaScript and jQuery - Web Technologies (1019888BNR)
Beat Signer
 
introduction of Data strutter and algirithm.pptx
ssuser7b3003
 
JavaScript in 2016
Codemotion
 
JavaScript in 2016 (Codemotion Rome)
Eduard Tomàs
 
Data Structures: A Foundation for Efficient Programming
MSridhar18
 
GeoGebra JavaScript CheatSheet
Jose Perez
 
CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version...
CodeFest
 
Chapter 10.ppt
MithuBose3
 
Module 2 Javascript. Advanced concepts of javascript
BKReddy3
 
07+08slide.pptx
MURADSANJOUM
 
Ad

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
L9. Math object in JS, CSE 202, BN11.pdf
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
 
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
 
L9. Math object in JS, CSE 202, BN11.pdf
SauravBarua11
 
L10. Math.random method in JS, CSE 202, BN11.pdf
SauravBarua11
 
L8. Constructor and method in JS, CSE 202, BN11.pdf
SauravBarua11
 
Ad

Recently uploaded (20)

PPT
Ppt for engineering students application on field effect
lakshmi.ec
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PDF
A Framework for Securing Personal Data Shared by Users on the Digital Platforms
ijcncjournal019
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
ghousebhasha2007
 
PDF
Structs to JSON How Go Powers REST APIs.pdf
Emily Achieng
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PDF
ETO & MEO Certificate of Competency Questions and Answers
Mahmoud Moghtaderi
 
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
VinayB68
 
PDF
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
quakeplayz54
 
PPTX
TE-AI-Unit VI notes using planning model
swatigaikwad6389
 
PDF
Queuing formulas to evaluate throughputs and servers
gptshubham
 
PPTX
Practice Questions on recent development part 1.pptx
JaspalSingh402
 
PDF
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
PPTX
Module_II_Data_Science_Project_Management.pptx
anshitanarain
 
PDF
Principles of Food Science and Nutritions
Dr. Yogesh Kumar Kosariya
 
PPT
High Data Link Control Protocol in Data Link Layer
shailajacse
 
PPTX
EE3303-EM-I 25.7.25 electrical machines.pptx
Nagen87
 
Ppt for engineering students application on field effect
lakshmi.ec
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
A Framework for Securing Personal Data Shared by Users on the Digital Platforms
ijcncjournal019
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
ghousebhasha2007
 
Structs to JSON How Go Powers REST APIs.pdf
Emily Achieng
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
ETO & MEO Certificate of Competency Questions and Answers
Mahmoud Moghtaderi
 
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
VinayB68
 
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
Lesson 3_Tessellation.pptx finite Mathematics
quakeplayz54
 
TE-AI-Unit VI notes using planning model
swatigaikwad6389
 
Queuing formulas to evaluate throughputs and servers
gptshubham
 
Practice Questions on recent development part 1.pptx
JaspalSingh402
 
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
Module_II_Data_Science_Project_Management.pptx
anshitanarain
 
Principles of Food Science and Nutritions
Dr. Yogesh Kumar Kosariya
 
High Data Link Control Protocol in Data Link Layer
shailajacse
 
EE3303-EM-I 25.7.25 electrical machines.pptx
Nagen87
 

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