Flashcards
Loops and Arrays
Cards in this deck (50)
- 1What are Arrays?-They are a type of Value that represents a list of values.
- 2Syntax of an array[10, 20, 30]
- 3Why can we save an array as a variable?They are also values
- 4Example of setting a variable equal to an arrayconst variable = [number, 'string', true, {name: 'sock'}, [1, 2,] ]
arrays can contain numbers, strings, Booleans, objects and arrays. - 5What index does the value 1 represent in the array [1, 3, 4] number 3 number 4index 0
index 1
index 2 - 6myArray = [ 10, 20, 30 ]; How can I change the value 10 to 99?set myArray[0]= 99
- 7console.log(typeof [1, 2]); What will the console show? Therefore?That an array is an object
arrays are objects - 8console.log(Array.isArray([1, 2])); What will the console show?True
- 9Since arrays are OBJECTS they have?properties and methods
- 10Example of an Array property?array.length;
- 11Example of an Array method?.push();
- 12let myArray = [1,2 ,4]; How can I add another value to the existing array? where is the value put?myArray.push(value);
[1, 2, 4, value] - at the end of the array - 13let myArray = [1,2 ,4]; How can I remove the value 1 at index 0 with a value of 2 What is this an example of?myArray.splice(0,2);
An array method - 14What' re the two types of loopsfor and while
- 15example of a while loop that counts from 1 to 5.let i = 1
while ( i <= 5) {
console.log(i)
i++;
}; - 16let i = 1 while ( i <= 5) { i++; }; What is the loop condition? What is the loop body? what is the Loop variable?1.) (i <= 5)
2.)
console.log(i);
i++;
3.) let i = 1 - 17let i = 1 while ( i <= 5) { i++; }; what is the increment step? What happens if we do not have this step?1.) i++
2.) the loop will keep going forever - 18let i = 1 while ( i <= 5) { }; what will happen? This is known as?1.) The loop will run forever.
2.) infinite loop - 19What is it called each time a loop is ran?iteration
- 20How many iterations will this loop have? let i = 1 while ( i <= 5) { i++; };5 iterations
- 21How can I use a loop to count from 1 to 5?for (let i = 1; i <= 5; i++) {
console.log(i);
} - 22What should we use for a standard loop?A for loop
- 23What is an example of a non-standard loop?let randomNumber = 0;
while (randomNumber < 0.5){
randomNumber = Math.random();
} - 24what will the console log? let randomNumber = 0; while (randomNumber < 0.5){ randomNumber = Math.random(); } console.log(randomNumber);a number that is greater than 0.5
- 25When should a while loop be used?non-standard loops
- 26How can I give the variable 'todoList' an array of strings 'make dinner', wash dishes', 'watch Youtube'const todoList = ['make dinner', 'wash dishes', 'watch Youtube']
- 27A for loop that counts the number of indexes of const todoList = ['make dinner', 'wash dishes', 'watch Youtube']for (let i = 0; i <= todoList.length-1; i++) {
console.log(i);
} - 28a for loop that displays each string in const toDoList = [ 'make dinner', 'wash dishes', 'watch youtube' ];for (i = 0; i < toDoList.length; i++){
const value = toDoList[i]
console.log(value);
} - 29First step of accumulator pattern?Create a variable to store the result
- 30second step of accumulator pattern?loop through the array and update the result.
- 31use an accumlator pattern to loop through the array to add up the sum const nums = [1, 2, 3]let total = 0;
for (let i = 0; i < nums.length; i++){
const num = numbs[i];
total =+ num;
}; - 32Why does the result variable declared with 'let'? let total = 0Because its going to be changing values during the iterations of the loop.
- 33What would the code const numbs = [1, 2, 3] let total = 0; constant num = numbs[0] total =+ num; console.log(total) display.1
- 34How can i use the accumulator pattern to double the values in const numbs = [1, 2, 3] and insert them into a new array called doubleNumbs?1.) set a variable to hold the values
doubleNumbs = [];
2.) Create a for loop that contains loop variable, conditions and increments
for (let i = 0; i < numbs.length; i++) {}
3.) set a variable = to the the incremental values
const num = numbs[i]
4.) push method the variable containing the incremental values into new array while multiplying it by 2.
doubleNumbs.push(num*2)
const doubleNumbs= [];
for (let i = 0; i < nums.length; i++){
const num = nums[i]
doubleNumbs.push(num*2)
}
console.log(doubleNumbs) - 35What is an example of Generating the HTML?for (let i = 0; i < todoList.length; i++){
const todo = todoList[i];
const html = `${todo}
`;
htmlList += html;
} - 36Create a function addNum(array, num) that takes an array of numbers and returns an array where each number is increased by 'num'.function addNum(array, num) {
result = [ ];
for (let i = 0; i < array.length; i++){
result.push(array[i]+num);
}
return result;
} - 37How can I remove an index from an array?.splice() method
- 38how could you use destructuring for the line of code const name = todoObject.name; const dueDate = todoObject.dueDate;const { name, dueDate } = todoObject;
- 39what does destructuring do?Takes a property from an object and sets it as a variable.
- 40What is the short-hand property syntax for name: nameyou can just use name
- 41Are arrays references like objects?yes
- 42when you write the code const array1 = [1, 2, 3]; const array2 = array1; what does array2 contain? both variables point to the same?it contains the reference of array1 within the computers memory, not the value [1, 2, 3];
array - 43const array1 = [1, 2, 3]; const array2 = array1; How can i get array2 to point to a different reference?const array2 = array1.slice();
- 44deconstruct array = [1, 2, 3] so that index 1 and 2 is set to variables firstValue and secondValueconst [firstValue, secondValue] = [1, 2, 3]
- 45what lets you exit a loop early?a break
- 46How do i exit the loop at the count of 8? for (let i = 1; i <= 10; i++) { console.log(i); }for (let i = 1; i <= 10; i++) {
console.log(i);
if (i === 8) {
break;
} - 47for (let i = 1; i <= 10; i++) { console.log(i); } How do I skip the third iteration?for (let i = 1; i <= 10; i++) {
if (i === 3) {
continue;
}
console.log(i) - 48for (let i = 1; i <= 10; i++) { console.log(i); } How do I skip every third iteration?for (let i = 1; i <= 10; i++) {
if (i % 3 === 0) {
continue;
} - 49How do I skip every third iteration in a while loop? let i = 10while (i <= 10) {
if (i % 3 === 0) {
i++
continue;
}
console.log(i);
i++
} - 50—
Information
- Description
- na Loops and Arrays Flashcards on Loops and Arrays, created by Brennen Bogue on 27/09/2023.
- Created by
- Brennen Bogue · Sep 2023
- Updated
- Sep 28, 2023
- Views
- 1
- Rating