Commit d0d10637 authored by Shanelle Valencia's avatar Shanelle Valencia

Add JS exercise

parents
function testButton() {
document.write("Why did you click me?");
}
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
<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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment