javascript
" aple ".trim()
"aple"
var s = " aple " undefined s = s.trim();
"aple" s + enter = "aple"
var dog = "allbertto" undefined dog.replace('l', z'); dog.replace('l', 'z'); *****replace double L with Z dog.replace(/l/g, 'z'); dog.replace('ll', 'z'); dog.replace('ll', 'zz'); "azzbertto"
"azlbertto" // first l will be affected "azbertto" // double ll will be affected "azzbertto" // double ll will be affected
var d = " dog" undefined d.trim()
"dog" d " dog"
Replace a to z var cat = "gato" undefined cat.replace('a', 'z') cat "gato" cat = cat.replace('a', 'z')
"gzto" "gzto" cat "gzto"
var wrd = "metallica"; wrd.substr(4,6);
"llica"
var another_wrd = "metallica-the-band" another_wrd.substr(4,6);
"llica-"
How to add to elements to the beginning of an array? The unshift() method adds new items to the beginning of an array, and returns the new length..
#######Example ######### var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.unshift("Lemon","Pineapple"); //Lemon,Pineapple,Banana,Orange,Apple,Mango
What does indexOf() do? The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
#######Example 1######### var a = [2, 9, 9]; a.indexOf(2); // 0 a.indexOf(7); // -1
What does reverse() do? Reverses an array
#######Example######### var a = ['one', 'two', 'three']; a.reverse(); console.log(a); // ['three', 'two', 'one']
What does split() do? Splits a string object into an array of strings by separating the string into substrings.
#######Example######### var myString = 'Hello World. How?'; var splits = myString.split(' '); console.log(splits); ["Hello", "World.", "How"]
"hello".indexOf('z') "hello".indexOf('h') "hello".indexOf('o') "hello".indexOf('l') "hello".indexOf(0)
-1 0 4 2 -1
console.log(true == true); console.log(false == false); console.log(true == false); console.log(false == true); //what prints out?
/* true true false false */
var a = 3; function stuff(){ var a = 2; } stuff(); console.log(a);
//3
function add(a,b){ console.log(a+b); } add(1,3); function add(a,b){ console.log(c); //variable hoisting console.log(a+b); var c = 3; } add(1,3);
//4 //4
var a = 3; function stuff(b){ var a = 2; console.log(a+b); } stuff(5);
//7
var a = 3; function stuff(b){ console.log(a+b); } stuff(5);
//8
if (1 == 1){ console.log('hi'); } //what prints out?
//Hi //because == only checks the value, not the type
write a function that takes in a letter and returns true if it is a vowel and false otherwise.
//answer function isVowel(let){ let = let.toLowerCase(); var vows = ['a', 'e', 'i', 'o', 'u', 'y']; return (vows.indexOf(let) > -1); } //another way function isVowel(let){ let = let.toLowerCase(); if (let == 'a' || let == 'e' || let == 'i' || let == 'o' || let == 'u' || let == 'y']){ return true; }else { return false; } } //another way function isVowel(let){ let = let.toLowerCase(); var vows = ['a', 'e', 'i', 'o', 'u', 'y']; for (var i=0; i<vows.length; i++){ if (vows[i] == let){ return true; } } return false; }
var fruit = "banana"; //print a random letter from fruit without explicitly typing out a letter
//answer fruit.substr(Math.floor(Math.random()*6), 1)
write a function that takes in a letter and returns true if it is a or b. If it is not a or b then return false.
//answer function isAOrB(let){ let = let.toLowerCase(); return (let == 'a' || let == 'b'); } //another way console.log('A' == 'a') //false console.log('A' === 'a') //false function isAOrB(let){ let = let.toLowerCase(); if (let == 'a' || let == 'b'){ return true; }else { return false; } } //another way function isAOrB(let){ let = let.toLowerCase(); if (let == 'a'){ return true; else if(let == 'b'){ return true; }else { return false; } }
var ob = { a : 1, b : 2 } Object.keys(ob) //["a", "b"] Object.values(ob) //[1, 2] question********* write a function that takes in a number and returns if it is even or odd
//answer function isEven(num){ if (num % 2 == 0){ console.log('is even'); }else{ console.log('is odd'); } } isEven(3); isEven(4); //another way function isEven(num){ if (num % 2 == 0){ return 'is even'; }else{ return 'is odd'; } } isEven(3); isEven(4); //another way function isEven(num){ return (num % 2 == 0); } var a = isEven(3); var b = isEven(4); a false b true
write a function that takes in an object and returns if one of the keys is named animal.
//answer function isKeyAnimal(ob){ return (Object.keys(ob).indexOf('animal') > -1); } //another way function isKeyAnimal(ob){ var keys = Object.keys(ob); for (var i=0; i<keys.length; i++){ if (keys[i] === 'animal'){ return true; } } return false; }
write a function that takes in an object and returns the number of keys inside of it
//answer function numKeys(ob){ return Object.keys(ob).length; } numKeys({b:2, c:3, d:8}); numKeys({fruit: apple, vege: carrot}); var person = { name: 'rich', age: 19 }; numKeys(person) //another way function numKeys(ob){ var count = 0; //value of count is initialized 0 for (key in ob){ count++; //count = count + 1; } return count; }
ani = "dog" console.log(ano);
//dragon
var ani = "dragon" var ano = ani; console.log(ano);
//dragon
console.log("apple".length == 3); //what prints out?
//false
if (1 === '1'){ console.log('hi'); } //what prints out?
//nothing prints out by checking the value and type
var f = "apple"; write code to console.log pl in the variable f without using the letters pl and using the variable f
//one way console.log(f[2] + f[3]); //another way console.log(f.slice(2,4))
take this array and create a new array of list items (li tags) with each element in it. Please use map. var arr = ['dog', 'cat', 'turtle'];
//one way var finished = arr.map(function(x){ return "<li>" + x + "</li>" }); //another way const finished = arr.map((x) => `<li>${x}</li>`); //this is what your code will do after it's done var finished = ["<li>dog</li>", "<li>cat</li>", "<li>turtle</li>"]
part C //how many times will this run? for (var i=1; i == animal.length; i++) { console.log(animal[i]); }
//prints Zero (0) times //because 1 does not equal the animal.length and then the for loop stops because of that
var words = ["dog", "cat", "lizard"]; loop through the words and print the word if it has an even length
//prints every word for (var i=0; i <= words.length-1; i++){ console.log(words[i]); } //prints even numbers for (var i=0; i <= words.length-1; i++){ if (words[i].length % 2 == 0){ console.log(words[i]); } } //prints even numbers another way for (var i=0; i < words.length; i++){ if (words[i].length % 2 == 0){ console.log(words[i]); } } //prints even numbers another way for (var i=0; i < words.length; i++){ var wrdLength = words[i].length; if (wrdLength % 2 == 0){ console.log(words[i]); } }
print the numbers 500 to 1 in that order using a for loop
//this prints 1 to 500 for (var i=1; i <= 500; i++){ console.log(i); }
print the numbers 1 to 500 in order using a for loop
//this prints 500 to 1 for (var i=500; i >= 1; i--){ console.log(i); } //this also prints 500 to 1 for (var i=500; i > 0; i--){ console.log(i); } //this also prints 500 to 1 for (var i=0; i < 500; i++){ console.log(500-i); }
var val = (4 == 4); console.log(val); //what prints out?
//true
console.log(this); //what prints out? function stuff(){ console.log(this); //what prints out? } stuff();
//window //window
4/2 2 4/3 1.3333333333333333 ??? 5/3 ????
4/3 1.3333333333333333 4 % 3 1 5 % 3 2
create a click BUTTON to wait one second, then alert Olumide greate job!
<!DOCTYPE html> <html> <body> <p> create a click BUTTON to wait one second, then alert Olumide greate job! </p> <button onClick="oluButton"> ClickMe </button> <script> function oluButton() { setTimeout(function(){ alert("Olumide great job!"); }, 1000); } </script> </body> </html>
var a = 3; function stuff(){ var a = 2; } stuff(); console.log(a);
ANSWER //3
var a = 3; function stuff(b){ //var b = 5; var a = 2; console.log(a+b); } stuff(5);
ANSWER //7
What is react?
JavaScript framework for UI Single Page Application Framework uses component architecture $('#container')
arr = [1,2,3]; var newArr = arr.filter(function(x){ return x != 2; }) newArr is what????
New Arry is [1,3]
function timesTwo(num){ console.log(num*2); } timesTwo(10) VM95:2 20 timesTwo(25) 50 var a = timesTwo(4); a undefined //a is undefined because we console logged num*2 in the function, we need to return it function timesTwo(num){ return num*2; } var a = timesTwo(4); 8
```//part 32 function timesTwo(num){ console.log(num*2); } timesTwo(10) VM95:2 20 timesTwo(25) 50 var a = timesTwo(4); a undefined //a is undefined because we console logged num*2 in the function, we need to return it function timesTwo(num){ return num*2; } var a = timesTwo(4); 8
part 13 part one var wrd = "legomyeggo"; //console.log eggo using the word wrd with substr wrd.substr(5, 10) ??????? wrd.substr(6, wrd.length-1) ?????? wrd.substr(6, 4) ???? wrd.substr(6, 0) ??????? wrd.substr(6, 1)
answer var wrd = "legomyeggo"; undefined wrd.substr(6, 10) "eggo" wrd.substr(5, 10) equals "yeggo" wrd.substr(6, wrd.length-1) = "eggo" wrd.substr(6, 4) === "eggo" wrd.substr(6, 0) === "" wrd.substr(6, 1) equals "e"
var a = 3; function stuff(b){ //var b = 5; console.log(a+b); } stuff(5);
answer //8
Add 4 into the array [1,2,3] var arr = [1,2,3] var copy = arr; arr.push(4) 4
arr (4) [1, 2, 3, 4] copy (4) [1, 2, 3, 4] var true_copy = arr.slice(0)
Add 5 into the array [1, 2, 3, 4] var arr = [1, 2, 3, 4] arr.push(5) 5
arr (5) [1, 2, 3, 4, 5] true_copy (4) [1, 2, 3, 4]
Why should we use react?
because as soon as you update the state, the document updates everywhere automatically as opposed to html, css, jQuery where if a variable updates, you have to update it everywhere
How do you clear a setTimeout?
clearTimeout(functionName) myVar = setTimeout(function(){ alert("Hello") }, 3000); clearTimeout(myVar);
The last position of any string is ________________________
console.log(a.length -1);
//loop through the words and print the last letter of each word
for (var i=0; i<words.length; i++){ var lastIndex = words[i].length - 1; console.log(words[i][lastIndex]); }
var words = ["desk", "phone", "screen"]; //loop through the words and print a random letter from each word
for (var i=0; i<words.length; i++){ var ranNum = Math.random()*words[i].length; var ranIndex = Math.floor(ranNum); console.log(words[i][ranIndex]); } /* words is an array of 3 strings words[0] is the word desk if I wanted to return the last the 2nd letter of desk, it would be e and that's index 1 so words[0][1] //that gives me e "desk"[1] -> e
loop through the numbers 1 to 100 and print out if they are even or odd
for (var i=1; i<101; i++){ if (i % 2 == 0){ console.log(i, 'even'); }else{ console.log(i, 'odd'); } } //another way for (var i=1; i<101; i++){ if (i % 2 != 0){ console.log('odd'); }else{ console.log('even'); } } //another way for (var i=1; i<=100; i++){ if (i % 2 == 0){ console.log('even'); }else{ console.log('odd'); } }
part two of part one loop over the variable (animal) and print each letter starting with the second letter and ending at the last letter.
for (var i=1; i<animal.length; i++) { console.log(animal[i]); } /* for (var i=1; i<=animal.length; i++) { console.log(animal[i]); } */
var animals = { cat : 'gato', dog : 'woofo', moose : 'mooseo' } //loop through the values of the animals object and print out the value if the length of the value is greater than 5.
for (var key in animals){ var val = animals[key]; //animals['cat'] -> 'gato' var valLength = val.length; //4 if (valLength > 5){ // if (4 > 5) ... console.log(val); } }
write a function that takes in two numbers and adds them together and returns the result if they are both even. If they are not then subtract them and return the result.
function addOrSub(a,b){ if ((a % 2 == 0 ) && (b % 2 == 0)){ return a+b; } else { return a-b; } }
var animal = "the giraffe says sup"; using indexOf console.log true if the word "sup" is in the animal variable and false otherwise using indexOf console.log true if the word "the" is in the animal variable and false otherwise.
if (animal.indexOf('sup') > -1){ console.log(true) }else{ console.log(false) } if (animal.indexOf('the') > -1){ console.log(true) }else{ console.log(false) }
arr = [1,2,3] newArr = arr.map((x) =>{ x*2; }) newArr will equal to ???
newArr will equal to [2,4,6]
Why do we use es6, es7...
react works with classic JavaScript, but all of the documentation, stock over flow posts, they're all using es6 arrow functions 'this' inside the arrow function is the same outside the arrow function
5 % 2 1 7 % 2 1 4 % 2 0 6 % 2 0 //any even number % 2 will give you 0 undefined 0 % 2
reminder 1 7 % 2 1 4 % 2 0 6 % 2 0 //any even number % 2 will give you 0 undefined 0 % 2 -5 % 2 -1 -7 % 3 -1 -7 % 4 -3
var val = true && true; var val_two = true && false; var val_three = true || true; var val_four = true || false; var val_five = false || true; var val_six = false || false; console.log(val, val_two, val_three, val_four, val_five, val_six); //what prints out?
true false true true true false
var val = 2; undefined var copy = val; undefined
val = 3; 3 copy 2
What does join() do? Joins all elements of an array into a string. #######Example#########
var a = ['Wind', 'Rain', 'Fire']; a.join(' + '); // 'Wind + Rain + Fire'
make a variable, put in the word "dog" into it print the second letter add the letter z to the variable print the variable print the length of the variable
var animal = "dog"; console.log(animal[1]); animal += "z"; // animal = animal + "z"; console.log(animal); console.log(animal.length);
var animal = "the fox says"; using indexOf console.log the index of the letter f + 1 in the animal variable
var animal = "the fox says"; console.log (animal.indexOf ('f') + 1);
What does concat() do? Combines the text of two strings or arrays
var arr1 = ['a', 'b', 'c']; var arr2 = ['d', 'e', 'f']; var arr3 = arr1.concat(arr2); // results in a new array [ "a", "b", "c", "d", "e", "f" ]
var arr = [1,2,3]; replace 2 with 4
var arrModified = arr.map(function(x){ if (x != 2) return x; else return 4; }); // [1,4,3] //arr is still [1,2,3]
use this array to return an array of even numbers using map [1,2,3,4,5,6]
var evenNums = [1,2,3,4,5,6].map(function(x){ return x*2; }); // Another Way to do it function retEvenNums(arr){ return arr.map(function(x){ return x*2; }); }
use the filter function to return an array of odd numbers from this array [1,2,3,4,5,6]
var oddNums = [1,2,3,4,5,6].filter(function(x){ return (x % 2 != 0); }); // Another way function retOddNums(arr){ return arr.filter(function(x){ return (x % 2 != 0); }); } // Another way var oddNums = retOddNums([1,2,3,4,5,6])
What does map() do? Iterates though array and preforms something to each element. Assigned to a new array. #######Example#########
var oldArray = [1, 2, 3]; var timesFour = oldArray.map(function(val){ return val * 4; }); console.log(timesFour); // returns [4, 8, 12] console.log(oldArray); // returns [1, 2, 3]
What does filter() do? Creates a new array with all the elements that pass the test implemented by the provided function. #######Example#########
var oldArray = [1,2,3,4,5,6,7,8,9,10]; var newArray = oldArray.filter(function(val){ return val < 6; }); //newArray = [1,2,3,4,5]
Random number between 0 to 5 excluded 0 and 5
var randBetZeroFive = Math.random()*5 undefined Math.floor(randBetZeroFive) 4 // 0 and 5 not included Math.ceil(randBetZeroFive) 5
What does replace() do? Changes a string by replacing a given parameter with another parameter. #######Example#########
var str = 'Twas the night before Xmas...'; var newstr = str.replace(/xmas/i, 'Christmas'); console.log(newstr); // Twas the night before Christmas
console.log the e in this word without using the letter e var word = "fiesta";
var word = "fiesta"; console.log (word[2]);
var wrd = "legomyeggo"; //console.log eggo using the word wrd with slice
var wrd = "legomyeggo"; undefined wrd.slice(6) "eggo"
//continue on with slice var wrd = "classroom" undefined wrd.slice(1) wrd.slice(5) wrd.slice(5,3) wrd.slice(5,7) "ro"
wrd.slice(1) equals "lassroom" wrd.slice(5) equals "room" wrd.slice (5,3) equals "" wrd.slice(5,7) equals "ro" //returns the letters at index 5 and 6