JAVASCRIPT

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

define JS string method search()

The search() method searches a string for a specified value and returns the position of the match: var str = "Please locate where 'locate' occurs!"; var pos = str.search("locate");

describe and give an example of the JS array method some()

The some() method check if some array values pass a test. example: var numbers = [45, 4, 9, 16, 25]; var someOver18 = numbers.some(myFunction); function myFunction(value, index, array) { return value > 18; }

when would you use an else if statement?

Use else if to specify a new condition to test, if the first condition is false

when would you use an else statement?

Use else to specify a block of code to be executed, if the same condition is false

when would you use an if statement?

Use if to specify a block of code to be executed, if a specified condition is true

arrays JS

You can create arrays in two different ways. The most common of which is to list values in a pair of square brackets. JavaScript arrays can contain any types of values and they can be of mixed types.

how would you find the highest number in an array?

You can use Math.max.apply to find the highest number in an array:

list the types of JS loops

for for/in for/of while do/while

syntax for a for loop in JS

for ([let i = startValue]; [i < endValue]; [i+=stepValue]) { // Loop code here }

example of a for loop in JS

for (let i = 0; i < 5; i++) { console.log(i); // Prints the numbers from 0 to 4 }

for loop syntax

for (statement 1; statement 2; statement 3) { // code block to be executed } Statement 1 is executed (one time) before the execution of the code block. Statement 2 defines the condition for executing the code block. Statement 3 is executed (every time) after the code block has been executed.

ex of else statement in JS

if (hour < 18) { greeting = "Good day"; } else { greeting = "Good evening"; } result: good day

example of JS else if statement

if (time < 10) { greeting = "Good morning"; } else if (time < 20) { greeting = "Good day"; } else { greeting = "Good evening"; } result = good morning

when should you use while loops in JS?

if you dont know how often you'll loop

example of a JS while loop

let x = 0; while (x < 5) { console.log(x); // Prints numbers from 0 to 4 x++; }

fixed values in JS are called....

literals

define the JS for loop

loops through a block of code a number of times

define the JS while loop

loops through a block of code while a specified condition is true

method example

makeBooking();

example of accessing an object method in JS

name = person.fullName();

in JS arrays are a type of ____

object

by default the JS sort() function sorts values as ____

strings

what invokes a function in js?

the () operator

give an example of how you can sort an array in descending order

var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); // First sort the elements of fruits fruits.reverse(); // Then reverse the order of the elements

example of JS .filter() method

var numbers = [45, 4, 9, 16, 25]; var over18 = numbers.filter(myFunction); function myFunction(value, index, array) { return value > 18;}

example of JS .map() method

var numbers1 = [45, 4, 9, 16, 25]; var numbers2 = numbers1.map(myFunction); function myFunction(value, index, array) { return value * 2; }

example of JS .reduce() method

var numbers1 = [45, 4, 9, 16, 25]; var sum = numbers1.reduce(myFunction); function myFunction(total, value, index, array) { return total + value;}

syntax for ternary operator

variablename = (condition) ? value1:value2

synatx for a while loop in JS

while (condition) { // Your code here }

JS objects are written w/ ___

{}

define object methods in JS

Objects can also have methods. Methods are actions that can be performed on objects. Methods are stored in properties as function definitions. function stored as aproperty

define .REPLACE() in JS

Returns a string with the first match substring replaced with a new substring.

define .INDEXOF() in JS

Returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex, Returns -1 if the value is not found. The .indexOf() method is case sensitive.

.length in JS

Returns the length of the string.

what does the following JS logical oporator mean? !expression5

Returns the opposite boolean value of the expression

what does the following JS logical oporator mean? expression1 && expression2

Returns true if both the expressions evaluate to true

what does the following JS logical oporator mean? expression3 || expression4

Returns true if either one of the expressions evaluates to true

define the do/while loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

describe the array method every()

The every() method check if all array values pass a test. example: var numbers = [45, 4, 9, 16, 25]; var allOver18 = numbers.every(myFunction); function myFunction(value) { return value > 18;}

describe the JS Array.filter()

The filter() method creates a new array with array elements that passes a test.

describe and give an example of the JS array method find()

The find() method returns the value of the first array element that passes a test function. example: var numbers = [4, 9, 16, 25, 29]; var first = numbers.find(myFunction); function myFunction(value, index, array) { return value > 18; }

describe the JS forEach() method

The forEach() method calls a function (a callback function) once for each array element.

describe the JS compare function

The purpose of the compare function is to define an alternative sort order. The compare function should return a negative, zero, or positive value, depending on the arguments: function(a, b){return a - b}

define JS array method push()

The push() method adds a new element to an array (at the end):

describe the JS Array.reduce() method

The reduce() method runs a function on each array element to produce (reduce it to) a single value. The reduce() method works from left-to-right in the array does not redduce the original array

describe the array method, reverse()

The reverse() method reverses the elements in an array.

syntax for classes in JS

SubClass.prototype = new SuperClass();

example of reassigning variable in JS

// Declare variable and give it value of 'Michael' let name = 'Michael' // Change the value of name to 'Samuel' name = 'Samuel'

define the JS string method split()

A string can be converted to an array with the split() method:

multidimensional arrays

A two-dimensional array is an array within an array. If you fill this array with another array you get a three-dimensional array and so on.

event handler in JS

Attributes of HTML tags embedded in documents. The attribute assigns a JavaScript command or function to execute when the event happens.

define function arguments

Function arguments are the values received by the function when it is invoked. Inside the function, the arguments (the parameters) behave as local variables.

function parameters are listed where?

Function parameters are listed inside the parentheses () in the function definition.

difference b/w JS arrays and objects

In JavaScript, arrays use numbered indexes. In JavaScript, objects use named indexes.

define the this keyword

In a function definition, this refers to the "owner" of the function.

JS if statements

It simply states that if this condition is true, do this, else do something else (or nothing). It occurs in varied forms.

describe the length property of an array

The length property of an array returns the length of an array (the number of array elements).

describe the JS Array.map()

The map() method creates a new array by performing a function on each array element. The map() method does not execute the function for array elements without values. The map() method does not change the original array.

define JS array method pop()

The pop() method removes the last element from an array:

define else statement in JS

a fallback to an if statement. This will only get executed if the previous statement did not

define the JS do/while loop

also loops through a block of code while a specified condition is true

x===y is a comparison operator that will...

return true if the 2 things are equal

x !== y is a comparison operator that will...

return true if two things are not equal

JS uses the ___ keyword to declare variables

var

in JS, declare a variable car assign the value "honda" to the car variable

var car; car = "honda"; or var car = "honda";

syntax for a JS function:

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().

describe classes in JS

A class can be thought of as a template to create many objects with similar qualities. Classes are a fundamental component of object-oriented programming (OOP).

example of if statement in JS

if (answer === 42) { console.log('Told you so!'); }

example of if else

if (condition) { // statement1: code that runs if condition is true } else { // statement2: code that runs if condition is false }

what;s the structure of and else if statement?

if (condition1) { statement1; } else if (condition2) { statement2; } else { statement3; }

when would you use a do while loop in JS?

if you have to loop at least once, but if you don't know how often.

describe an object in JS

is an aggregation (i.e., collection) of variables and functions. Ideally, the components of an object are put together in such a way that the object represents both the attributes and behavior of some "thing" being modeled in the program.

example of a JS do while loop

let x = 0; do { console.log(x); // Prints numbers from 0 to 4 x++; } while (x < 5);

in JS object properties are written as....

name:value pairs, separated by commas.

!== means what in JS?

not equal value or not equal type

whats the result of: if (!false && ( false || (false && true) )) { console.log('Guess what...'); }

not executed because: !false && ( false || (false && true) ) - becomes !false && ( false || false) - becomes true && false , which is false.

what does OOP stand for?

object oriented programming

how do you access object methods in JS?

objectName.methodName()

how do you acccess object properties in JS?

objectName.propertyName OR objectName["propertyName"]

what's a Javascript function?

a set of statements that performs a task or calculates a value. it's like a reusable piece of code. to use a function you must define it somewhere in the scope from which you wish to call it a function definition consists of the function keyword followed by the name of the function, a list of arguments to the function, enclosed in parenthases and separated by commas, the JS statements that define the function, enclosed in curly braces, {}

define the JS array method unshift()

adds a new element to an array (at the beginning), and "unshifts" older elements:

he safest way to loop through an array, is using a ____

for loop:

define primitivedata value

A primitive data value is a single simple data value with no additional properties and methods.

when to use arrays in JS and when to use objects

JavaScript does not support associative arrays. You should use objects when you want the element names to be strings (text). You should use arrays when you want the element names to be numbers.

Define JS method

Methods are functions that belong to objects, and for that reason they are sometimes called "member functions." Member functions are designed to operate directly on the properties of the object to which they belong.

describe an object's key value pair

Objects contain one or more key-value pairs. The key portion can be any string. The value portion can be any type of value: a number, a string, an array, a function, or even another object. When one of these values is a function, it's called a method of the object.. Otherwise, they are called properties.

define parseFloat in JS

Parses a string argument and returns a floating-point number if the first character is a plus sign, minus sign, decimal point, exponent, or a numeral. If it encounters a character other than one of the valid choices after that point, it returns the value up to that location and ignores all succeeding characters.

define parseInt in JS

Parses a string argument and returns an integer based on a specified radix or base. A radix of 10 converts the value to a decimal, while eight converts to octal, and 16 to hexadecimal. Values greater than 10 for bases above 10 are represented with letters (A through F) in place of numbers

describe the JS array method shift()

Shifting is equivalent to popping, working on the first element instead of the last. The shift() method removes the first array element and "shifts" all other elements to a lower index.

describe the 4 different statements of a for loop: for ([initialisation]; [conditional]; [iteration])[loopBody]

The initialisation statement is executed only once, before the loop starts. It gives you an opportunity to prepare or declare any variables. The conditional statement is executed before each iteration, and its return value decides whether or not the loop is to continue. If the conditional statement evaluates to a falsey value then the loop stops. The iteration statement is executed at the end of each iteration and gives you an opportunity to change the state of important variables. Typically, this will involve incrementing or decrementing a counter and thus bringing the loop ever closer to its end. The loopBody statement is what runs on every iteration. It can contain anything you want. You'll typically have multiple statements that need to be executed and so will wrap them in a block ( {...}).

array method join() does what?

The join() method also joins all array elements into a string.

define the array method length

The length property provides an easy way to append a new element to an array: var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits[fruits.length] = "Kiwi"; // Appends "Kiwi" to fruits

describe the JS string method replace()

The replace() method replaces a specified value with another value in a string: str = "Please visit Microsoft!"; var n = str.replace("Microsoft", "W3Schools"); doesn't change the string it is called on. returns a new string By default, the replace() method replaces only the first match:

define the array method slice()

The slice() method slices out a piece of an array into a new array. creates a new array. It does not remove any elements from the source array. this method can take two arguments like slice(1, 3).

describe the array method sort()

The sort() method sorts an array alphabetically:

define the array method splice()

The splice() method can be used to add new items to an array: ex: var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2, 0, "Lemon", "Kiwi"); The first parameter (2) defines the position where new elements should be added (spliced in). The second parameter (0) defines how many elements should be removed. The rest of the parameters ("Lemon" , "Kiwi") define the new elements to be added.

define ternary operators in JS

The ternary operator is usually used as a shortcut for the if statement.

define Else If in JS

This is like an else statement, but with its own condition. It will only run if its condition is true, and the previous statement's condition was false.

describe local variables in JS

Variables declared within a JavaScript function, become LOCAL to the function. Local variables can only be accessed from within the function. Since local variables are only recognized inside their functions, variables with the same name can be used in different functions. Local variables are created when a function starts, and deleted when the function is completed.

why are JS functions so useful?

You can reuse code: Define the code once, and use it many times. You can use the same code many times with different arguments, to produce different results.

for loops JS

You use for loops, if you know how often you'll loop. The most often used varName in loops is i.

describe the JS concat() method

concat() joins two or more strings: The concat() method can be used instead of the plus operator.

syntax for ternary operators in JS

condition ? expr1 : expr2

syntax of a do while loop

do { // Your code here } while (condition);

calling a method of an object example

document.write('Good afternoon!'); ( document = object. = member operator' Good afternoon!' = parameters write('Good afternoon!'); = method )

=== in js

does both value checking and type checking

== in JS

does just value checking (no type checking ),

if var i = 1 var j = ++i; (meaning...)

pre-increment: j equals 2; i equals 2

objects in JS can have what 2 things?

properties or methods

The easiest way to add a new element to an array is using the______

push() method:

describe the JS string method slice()

slice() extracts a part of a string and returns the extracted part in a new string. The method takes 2 parameters: the start position, and the end position (end not included).

what are the 3 methods for extracting a part of a string in JS?

slice(start, end) substring(start, end) substr(start, length)

what happens when JS reaches a return statement?

the function will stop executing If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement. Functions often compute a return value. The return value is "returned" back to the "caller"

describe instance in JS

the instance of a command is contained in parentheses immediately following the command. The Instance contains information about what an object is to do or how a method is to be carried out.

how are JS types dynamic?

the same variable can be used to hold different data types

describe JS properties

the variables that makes up an object are called the object's properties. In other words, properties are variables that belongs to an object; thus they are sometimes called member variables. Other than the fact that properties belong to objects, there is little behavioral difference between variables and properties. However, a property's name alone is insufficient to identify an object's property. In JavaScript, properties are identified by concatenating the object and property names together, with the object name first and the two names separated by a dot (period).

describe JS variables

they're typeless. This means that any variable can take on any type of data -- numbers, strings, arrays, objects, etc. Because JavaScript variables are not restricted to specific types of values, there is no need to "declare" them before using them. Instead, memory is automatically allocated to store the variable when the variable is assigned its first value.

whats the syntax for a simple variable declaration in JS?

var foo = 'hello world';

example of the JS array method splice()

var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2, 2, "Lemon", "Kiwi");

example of the JS array method shift()

var fruits = ["Banana", "Orange", "Apple", "Mango"]; var x = fruits.shift(); // the value of x is "Banana"

example of an object definition

var person = { firstName: "John", lastName: "Doe", age: 50, eyeColor: "blue"};

example of the JS slice method

var str = "Apple, Banana, Kiwi"; var res = str.slice(7, 13); result of res would be: banana

example of split method in JS

var txt = "Hello"; // String txt.split(""); // Split in characters

JavaScript ___ are containers for data values.

variables

Accessing a function without () in JS will do what?

will return the function definition instead of the function result:

in JS strings can be defined as objects with....

with the keyword new: var firstName = new String("John");


Ensembles d'études connexes

MGT 340- Organizational Behavior Final Exam Review

View Set

Spanish 12 Chapter 9 Car Accident Vocabulary

View Set

Chapter 49: Immunologic Agents and Chapter 51: Immunomodulating Drugs

View Set

Prep U- Chapter 58: Assessment & Management of Patients With Breast Disorders

View Set

Prin. Bus. Fin. Modul 1 "Building A Balanced Budget" { Mr. Bayaborda }

View Set

System Analysis and Design: Project Management (CH4)

View Set