What data types can you store inside of an array?
- Number: These are numerical values such as 1, 2, 3 they are also sometimes referred to as integers.
- Strings: These are variables that hold a value of a text, such as a list for grocery shopping where we write bread, eggs, milk, cheese, etc.
- Objects: Such as true/false values like with boolean situations
- Other Arrays: Yup you got that right, even other arrays can be stored inside an array. This is most commonly used when trying to create tables of information versus just a list.
Is the people array a valid Javascript array? If so, how can i accss the values stored? If not, why?
const people = [['pete', 32, 'librian', null], ['smith', 40, 'accountant', 'fishing: hiking: rock_climbing'], ['bill', null, 'artist', null]];
Yes this is a valid array and it can be accessed by first deciding which cell you wish to access such as 0 being the first, keep in mind all arrays start off at the number 0 for access. So [0] would access the first item in the whole array, which is the first bracket of information then we would select an array item between 0 and 3 so if we wanted librarian we would do select the array known as people([0][2]). This will access the first cell in the overall array which holds a second array, and the 2 will access the third item in that array.
List five shorthand operators for assignment in javascript and describe what they do.
- ' + ' is an operator for adding one value to another.
- ' - ' is similar to the addition operator it instead subtracts a value from another.
- ' = ' is the assignment operator it assigns a value to a variable determined by the developer.
- ' += ' is the operator used for addition assignment, what this means is it will add the corresponding value and reassign the original variable to this new value.
- ' %= ' is the remainder assigment operator, it takes the remainder of two operands and assigns the remaining value to the left operand.
let a = 10; let b = 'dog'; let c = false; evaluate (a+c) + b; The overall result would be 10dog because there is no conditional reasoning to justify a callback to anything being false or true, so while it may technically add all of the elements above, false is not a value that can be displayed without any form of conditional actions to give true or false.
Describe a real world example of when a conditional statement should be used in a javascript program.
An obvious answer to this would be based on a user input is where a conditional statement should be used. If the user does X action run X code, else if user does Y instead run Y code instead.
Give an example of when a loop is useful in javascript.
Loops are useful for when you want a specific set of code to run until a certain condition is met. For example if a user doesn't input the right password then keep asking for the right password until it's entered correctly or a limit of attempts is reached.