You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
182 lines
5.6 KiB
JavaScript
182 lines
5.6 KiB
JavaScript
1 year ago
|
// console.log("Hello World");
|
||
|
|
||
|
// [Section] While Loop
|
||
|
/*
|
||
|
- A while loop takes in an expression/condition
|
||
|
- Expressions are any unit of code that can be evaluated to a value
|
||
|
- If the condition evaluates to true, the statements inside the code block will be executed
|
||
|
- A statement is a command that the programmer gives to the computer
|
||
|
- A loop will iterate a certain number of times until an expression/condition is met
|
||
|
- "Iteration" is the term given to the repetition of statements
|
||
|
- Syntax
|
||
|
while(expression/condition) {
|
||
|
statement
|
||
|
}
|
||
|
*/
|
||
|
|
||
|
|
||
|
let count = 5;
|
||
|
|
||
|
// While the value of count is not equal to 0
|
||
|
while(count !== 0) {
|
||
|
|
||
|
// The current value of count is printed out
|
||
|
console.log("While: " + count);
|
||
|
|
||
|
// Decreases the value of count by 1 after every iteration to stop the loop when it reaches 0
|
||
|
// Loops occupy a significant amount of memory space in our devices
|
||
|
// Make sure that expressions/conditions in loops have their corresponding increment/decrement operators to stop the loop
|
||
|
// Forgetting to include this in loops will make our applications run an infinite loop which will eventually crash our devices
|
||
|
// After running the script, if a slow response from the browser is experienced or an infinite loop is seen in the console quickly close the application/browser/tab to avoid this
|
||
|
count--;
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
// [Section] Do While Loop
|
||
|
/*
|
||
|
- A do-while loop works a lot like the while loop. But unlike while loops, do-while loops guarantee that the code will be executed at least once.
|
||
|
- Syntax
|
||
|
do {
|
||
|
statement
|
||
|
} while (expression/condition)
|
||
|
*/
|
||
|
|
||
|
/*
|
||
|
- The "Number" function works similarly to the "parseInt" function
|
||
|
- Both differ significantly in terms of the processes they undertake in converting information into a number data type and other features that help with manipulating data
|
||
|
- The "prompt" function creates a pop-up message in the browser that can be used to gather user input
|
||
|
- How the Do While Loop works:
|
||
|
1. The statements in the "do" block executes once
|
||
|
2. The message "Do While: " + number will be printed out in the console
|
||
|
3. After executing once, the while statement will evaluate whether to run the next iteration of the loop based on given expression/condition (e.g. number less than 10)
|
||
|
4. If the expression/condition is not true, another iteration of the loop will be executed and will be repeated until the condition is met
|
||
|
5. If the expression/condition is true, the loop will stop
|
||
|
*/
|
||
|
|
||
|
|
||
|
// let number = Number(prompt("Give me a number"));
|
||
|
|
||
|
// do {
|
||
|
|
||
|
// // The current value of number is printed out
|
||
|
// console.log("Do While: " + number);
|
||
|
|
||
|
// // Increases the value of number by 1 after every iteration to stop the loop when it reaches 10 or greater
|
||
|
// // number = number + 1
|
||
|
// number += 1;
|
||
|
|
||
|
// // Providing a number of 10 or greater will run the code block once and will stop the loop
|
||
|
// } while (number < 10)
|
||
|
|
||
|
|
||
|
// [Section] For Loop
|
||
|
/*
|
||
|
- A for loop is more flexible than while and do-while loops. It consists of three parts:
|
||
|
1. The "initialization" value that will track the progression of the loop.
|
||
|
2. The "expression/condition" that will be evaluated which will determine whether the loop will run one more time.
|
||
|
3. The "finalExpression" indicates how to advance the loop.
|
||
|
- Syntax
|
||
|
for (initialization; expression/condition; finalExpression) {
|
||
|
statement
|
||
|
}
|
||
|
*/
|
||
|
|
||
|
/*
|
||
|
- Will create a loop that will start from 0 and end at 20
|
||
|
- Every iteration of the loop, the value of count will be checked if it is equal or less than 20
|
||
|
- If the value of count is less than or equal to 20 the statement inside of the loop will execute
|
||
|
- The value of count will be incremented by one for each iteration
|
||
|
*/
|
||
|
|
||
|
|
||
|
for (let count = 0; count <= 20; count++) {
|
||
|
|
||
|
// The current value of count is printed out
|
||
|
console.log(count);
|
||
|
|
||
|
}
|
||
|
|
||
|
// for (let count = 0; count >= -20; count--) {
|
||
|
// console.log(count);
|
||
|
// }
|
||
|
|
||
|
let myString = "alex";
|
||
|
|
||
|
console.log(myString.length);
|
||
|
|
||
|
// Accessing index number
|
||
|
|
||
|
console.log(myString[0]);
|
||
|
console.log(myString[1]);
|
||
|
console.log(myString[2]);
|
||
|
|
||
|
for(let x = 0; x < myString.length; x++) {
|
||
|
|
||
|
console.log(myString[x])
|
||
|
}
|
||
|
|
||
|
let myName = "AlEx";
|
||
|
|
||
|
for (let i=0; i < myName.length; i++) {
|
||
|
|
||
|
|
||
|
if (
|
||
|
myName[i].toLowerCase() == "a" ||
|
||
|
myName[i].toLowerCase() == "i" ||
|
||
|
myName[i].toLowerCase() == "u" ||
|
||
|
myName[i].toLowerCase() == "e" ||
|
||
|
myName[i].toLowerCase() == "o"
|
||
|
){
|
||
|
// If the letter in the name is a vowel, it will print the number 3
|
||
|
console.log(3);
|
||
|
} else {
|
||
|
// Print in the console all non-vowel characters in the name
|
||
|
console.log(myName[i])
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// [SECTION] Continue and Break statements
|
||
|
|
||
|
for (let count = 0; count <= 20; count++) {
|
||
|
|
||
|
// if remainder is equal to 0
|
||
|
if (count % 2 === 0) {
|
||
|
|
||
|
// Tells the code to continue to the next iteration of the loop
|
||
|
// This ignores all statements located after the continue statement;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// The current value of number is printed out if the remainder is not equal to 0
|
||
|
console.log("Continue and Break: " + count);
|
||
|
|
||
|
// If the current value of count is greater than 10
|
||
|
if (count > 10) {
|
||
|
|
||
|
// Tells the code to terminate/stop the loop even if the expression/condition of the loop defines that it should execue so long as the value of count is less than or equal to 20
|
||
|
// number values after 10 will no longer be printed
|
||
|
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
let name = "alexandro";
|
||
|
|
||
|
|
||
|
for (let i = 0; i < name.length; i++) {
|
||
|
|
||
|
console.log(name[i]);
|
||
|
|
||
|
if (name[i].toLowerCase() === "a") {
|
||
|
console.log("Continue to the next iteration");
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if (name[i] == "d") {
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
}
|