Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
javascript-exercise
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shanelle Valencia
javascript-exercise
Commits
d0d10637
Commit
d0d10637
authored
Apr 09, 2021
by
Shanelle Valencia
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add JS exercise
parents
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
158 additions
and
0 deletions
+158
-0
buttonFunc.js
buttonFunc.js
+4
-0
loops.js
loops.js
+80
-0
practice.html
practice.html
+74
-0
No files found.
buttonFunc.js
0 → 100644
View file @
d0d10637
function
testButton
()
{
document
.
write
(
"Why did you click me?"
);
}
loops.js
0 → 100644
View file @
d0d10637
function
forInLoop
(
array
)
{
let
res
=
[];
for
(
const
idx
in
array
)
{
if
(
idx
%
2
==
0
)
{
res
.
push
(
idx
);
}
}
return
res
;
}
function
forOfLoop
(
arr
)
{
let
res
=
[];
for
(
const
ele
of
arr
)
{
if
(
ele
%
2
===
0
)
{
res
.
push
(
ele
);
}
}
return
res
;
}
function
doWhileLoop
()
{
let
i
=
0
;
let
numStr
=
''
;
do
{
i
++
;
numStr
+=
(
`
${
i
}
`
);
}
while
(
i
<=
7
);
return
numStr
;
}
function
whileLoop
()
{
let
i
=
0
;
let
numStr
=
''
;
while
(
i
<=
7
)
{
numStr
+=
(
`
${
i
}
`
);
i
++
;
}
return
numStr
;
}
function
switchStatements
(
arr
)
{
const
randomNum
=
arr
[
Math
.
floor
(
arr
.
length
*
Math
.
random
())];
switch
(
randomNum
)
{
case
"exercise"
:
return
(
`
${
randomNum
}
: Yikes`
);
case
"sleep"
:
return
(
`
${
randomNum
}
: yes please`
);
case
"code"
:
return
(
`
${
randomNum
}
: breathe code 24/7`
);
case
"eat"
:
return
(
`
${
randomNum
}
: all day`
);
default
:
return
(
`
${
randomNum
}
: error`
);
}
}
const
arr
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
];
const
strArr
=
[
"exercise"
,
"sleep"
,
"code"
,
"eat"
];
console
.
log
(
forInLoop
(
arr
));
console
.
log
(
forOfLoop
(
arr
));
console
.
log
(
doWhileLoop
());
console
.
log
(
whileLoop
());
console
.
log
(
switchStatements
(
strArr
));
\ No newline at end of file
practice.html
0 → 100644
View file @
d0d10637
<html>
<head>
<title>
My First Program
</title>
<script
language =
"javascript"
type =
"text/javascript"
>
let
globalVar
=
"global"
;
function
variableScopes
()
{
let
localVar
=
"local"
;
document
.
write
(
localVar
);
}
</script>
<script
language =
"javascript"
type =
"text/javascript"
>
function
moodRandomizer
()
{
const
moods
=
[
"happy 😊"
,
"sad 😢"
,
"sleepy 🥱"
,
"hangry 😡"
,
"cool 😎"
,
"lost 🤯"
,
"Friday vibez"
]
const
randomIdx
=
Math
.
floor
(
moods
.
length
*
Math
.
random
())
return
moods
[
randomIdx
];
};
function
todaysMood
(
name
)
{
myMood
=
moodRandomizer
();
document
.
write
(
`
${
name
}
is feeling
${
myMood
}
today.`
);
if
(
myMood
===
"Friday vibez"
)
{
alert
(
"FRIDAY MOOD: 🕺🏻🕺🏻🕺🏻"
);
}
}
</script>
<script
type=
"text/javascript"
src=
"buttonFunc.js"
></script>
<script
type=
"text/javascript"
src=
"loops.js"
>
const
arr
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
];
const
strArr
=
[
"exercise"
,
"sleep"
,
"code"
,
"eat"
];
</script>
</head>
<body>
<h1>
Practice
</h1>
<h2>
<marquee
behavior=
"alternate"
direction=
""
style=
"width: 500px"
>
<script>
((
name
,
session
)
=>
{
document
.
write
(
`Hello
${
name
}
! Welcome to
${
session
}
.`
);
})(
"Tester"
,
"JS Basics"
);
</script>
</marquee>
</h2>
<script>
document
.
write
(
globalVar
);
document
.
write
(
"<br>"
);
variableScopes
();
document
.
write
(
localVar
);
//ref error due to scope - will not render
</script>
<h3>
<script>
todaysMood
(
"Shanel"
);
</script>
</h3>
<script>
document
.
write
(
doWhileLoop
(
arr
)
+
'<br>'
+
switchStatements
(
strArr
));
</script>
<br>
<input
type=
"button"
onclick=
"testButton()"
value=
"Click Me"
>
</body>
</html>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment