FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
CCDATRCL/ProblemSet/Week 2/Problem/JavaScript/Array.js at main · flexycode/CCDATRCL · GitHub
flexycode
/
CCDATRCL
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
9
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
CCDATRCL
/
ProblemSet
/
Week 2
/
Problem
/
JavaScript
/
Array.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
78 lines (70 loc) · 2.54 KB
Breadcrumbs
CCDATRCL
/
ProblemSet
/
Week 2
/
Problem
/
JavaScript
/
Array.js
Copy path
File metadata and controls
78 lines (70 loc) · 2.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function
main
(
)
{
// Ask the user to enter the size of the array
let
size
=
parseInt
(
prompt
(
"Enter the size of the array: "
)
)
;
// Declare and initialize an array of integers with the specified size
let
scores
=
[
]
;
// Ask the user to enter the values for the array elements
console
.
log
(
"Enter the values for the array elements:"
)
;
for
(
let
i
=
0
;
i
<
size
;
i
++
)
{
while
(
true
)
{
let
score
=
prompt
(
`Element
${
i
+
1
}
: `
)
;
if
(
!
isNaN
(
score
)
)
{
scores
.
push
(
parseInt
(
score
)
)
;
break
;
}
else
{
console
.
log
(
"Invalid input. Please enter a valid integer."
)
;
}
}
}
// Print the array elements
console
.
log
(
"Array elements:"
)
;
for
(
let
score
of
scores
)
{
console
.
log
(
score
+
" "
)
;
}
console
.
log
(
)
;
// Ask the user if they want to add or remove an element from the array
while
(
true
)
{
let
inputStr
=
prompt
(
"Do you want to add or remove an element from the array? (add/remove): "
)
;
if
(
inputStr
.
toLowerCase
(
)
===
"add"
||
inputStr
.
toLowerCase
(
)
===
"remove"
)
{
break
;
}
else
{
console
.
log
(
"Invalid input. Please enter 'add' or 'remove'."
)
;
}
}
if
(
inputStr
.
toLowerCase
(
)
===
"add"
)
{
// Ask the user to enter the new element
while
(
true
)
{
let
newElement
=
prompt
(
"Enter the new element: "
)
;
if
(
!
isNaN
(
newElement
)
)
{
scores
.
push
(
parseInt
(
newElement
)
)
;
break
;
}
else
{
console
.
log
(
"Invalid input. Please enter a valid integer."
)
;
}
}
// Print the updated array elements
console
.
log
(
"Updated array elements:"
)
;
for
(
let
score
of
scores
)
{
console
.
log
(
score
+
" "
)
;
}
console
.
log
(
)
;
}
else
if
(
inputStr
.
toLowerCase
(
)
===
"remove"
)
{
// Ask the user to enter the index of the element to remove
while
(
true
)
{
let
index
=
prompt
(
"Enter the index of the element to remove: "
)
;
if
(
!
isNaN
(
index
)
&&
0
<=
parseInt
(
index
)
<
scores
.
length
)
{
scores
.
splice
(
parseInt
(
index
)
,
1
)
;
break
;
}
else
{
console
.
log
(
"Invalid index. Please enter a valid index."
)
;
}
}
// Print the updated array elements
console
.
log
(
"Updated array elements:"
)
;
for
(
let
score
of
scores
)
{
console
.
log
(
score
+
" "
)
;
}
console
.
log
(
)
;
}
}
main
(
)
;
Back
|
FazBrowse Home
|
New Git URL