// Topic: Arrays /* Topics Covered: - Let, Const -> Fixed Arrays - Reference to Origin - CallBack & Higher Order Functions - MAP | Find | Filter - Some and Every - Ternary Operator - Arrow Function - Array Sort (sort numbers , strings) - Spread */ //* Primitive Datatypes vs Reference Datatypes // const bank = 250; // bank = 100; // console.log(bank); //Uncaught TypeError: Assignment to constant variable. /* - Changes can be made in arrays when using const which we could not do when using const - in primitive ddta types - if we change the entire array then it will throw error . modification can be made but - not entire array */ // const names = ["Bill"]; // const newArray = ["khan"]; // names[0] = "KhanX"; // names.push(newArray[0]); // names = []; // Uncaught TypeError: Assignment to constant variable. // console.log(names); /* * When using LET - In primitive data types, we can change the value of the variable - In reference data types ARRAYS AND OBEJCTS, even if we change the value in the new assigned variable, - the original array will also change - original array. because of reference */ /* Example of usin LET let bank = 250; let newbank = 250; console.log(bank, newbank); newbank = 500; console.log(bank, newbank); const names = ["khan", "julie", "kevin"]; const newNames = names; newNames.push("hard"); console.log(newNames); // change array console.log(names); // original array but get modified because of reference const tweet = { name: "khan", tweets: 15, age: 26, }; const newTweet = tweet; newTweet.name = "Khan Baba"; console.log(tweet); console.log(newTweet); */ // Topic: CALLBACK & HIGHER ORDER FUNCTIONS /* videos.forEach(function (video) - Function that takes another function as a parameter is called higher order function - Callback function is a function that executes based on event */ /* - ForEach simply loops over the array but does not actually return us anything const videos = ["hi", "jennifer", "watch TV"]; videos.push("marvin"); console.log(videos); * example of higher order callback function videos.forEach(function (video) { console.log(video.length); console.log(video.slice(1, video.length)); }); // Another Example of higher order function function repeater(fn) { fn(); fn(); } function hello() { console.log("Hello"); } repeater(hello); // anonymous higher order function repeater(function () { console.log(`Helloooo`); }); */ // Topic: MAP, Find,Filter /* - MAP : Create a copy of the array , loop over it and can run a function for modification - MAP always return us a value array.map(function(currentValue, index, arr), thisValue) * function(currentValue, index, arr): It is required parameter and it runs on each element of array. It contains three parameters which are listed below: * currentValue: It is required parameter and it holds the value of current element. * index: It is optional parameter and it holds the index of current element. * arr: It is optional parameter and it holds the array. * thisValue: It is optional parameter and used to hold the value of passed to the function. */ /* const videos = [ "I love apples", "HTML5", "country is just a love land", "could js take over every application", ]; const mapvideos = videos.map((video, index) => { console.log(video, index); return video.toUpperCase(); }); console.log(`Original Array: ${videos}`); //Original Array : Stays same ["I love apples","country is just a land","could js take over every application" ]; console.log(`Map Array : ${mapvideos}`); // ["I LOVE APPLES", "COUNTRY IS JUST A LAND", "COULD JS TAKE OVER EVERY APPLICATION"] */ // - Find /* - Find : loop over an array and returns only the value/string that we are looking for. - Only the first matched value */ /* const findvideos = videos.find(function (video) { return video.includes("love"); }); console.log(`Find Array : ${findvideos}`); // "I love apples", */ //- Filter /* - Filter : loop over an array and returns all the values/strings that we are looking for. */ /* const filtervideos = videos.filter(function (video) { return video.length < 10; }); console.log(`Filter Array : ${filtervideos}`); //["I love apples", "country is just a love land",] const games = [ { title: "GTA", rating: 9 }, { title: "GTA4", rating: 8 }, { title: "CAll of duty", rating: 5 }, { title: "MOHA", rating: 6 }, ]; const filtergames = games.filter(function (game) { if (game.rating < 7) { return console.log(game.rating); } else { return game.title; } }); console.log(filtergames); */ // Topic: Some and Every /* - Only return us either a true or false value - Every: it is like && . if all values satisfies the condition return true - Some: If only value satisfies , return true else false */ /* const games = [ { title: "GTA", rating: 9 }, { title: "GTA4", rating: 8 }, { title: "CAll of duty", rating: 5 }, { title: "MOHA", rating: 6 }, ]; const everyRating = games.every(function (game) { return game.rating > 5; }); const someRating = games.some(function (game) { return game.rating > 8; }); console.log(everyRating); console.log(someRating); */ // Topic: Ternary Operator /* - More like a If statement - condition ? do this : else this */ /* const videos = [ "I love apples", "html5", "country is just a love land", "could js take over every application", ]; const ternaryVid = videos.map(function (video) { return video.length < 10 ? video : "greater than"; // if (video.length < 8) { // return video; // } else { // return "greater than"; // } }); console.log(ternaryVid); // ["greater than", "html5", "greater than", "greater than"] */ // Topic:Arrow Function /* - As we use a ton of callback and anonymous function, it is best to use arrow function - Normal way const everyRating = games.every( (game)=> { return game.rating > 5; }); - Shortened way: when only need to return a value , no need for comlex math like assigning new variable - Then there is no need for return as it assuedmed that after => then next line will be returned - const everyRating = games.every( (game)=> game.rating > 5;); */ // Topic: SORT /* - Sort items in an array - SOrtin is somewhat wierd in JS as it sorts string fine but not well when it comes to numbers - because the numbers are first converted to string then compare results in wierd behavior - The best way to solve this is to use a function which returns a-b . - a compare function which returns either negative 0 or positive value. - if result negative then a will be sorted before b . - if the result is positive then b wll be sorted before a - if both values same then nothing changes const rating = [5, 2, 4, 45, 22, 3, 1]; a = 5 b = 2 [2, 5, 4, 45, 22, 3, 1]; rating.sort((a, b) => { console.log(a , b); return a - b; // ascending a-b // descending b-a }); */ // const items = ["Apple", "Banana", "citrus", "Mango"]; // const rating = [5, 2, 4, 45, 22, 3, 1]; // items.sort(); // rating.sort((a, b) => { // return a - b; // ascending a-b // descending b-a // }); // console.log(items); // console.log(rating); // const games = [ // { title: "GTA", rating: 9 }, // { title: "GTAVI", rating: 8 }, // { title: "CAll of duty", rating: 5 }, // { title: "MOHA", rating: 6 }, // ]; // games.sort((a, b) => { // return a.rating - b.rating ; // }); // console.log(games); //Result console.log(games); /* (4) [{…}, {…}, {…}, {…}] 0: {title: "CAll of duty", rating: 5} 1: {title: "MOHA", rating: 6} 2: {title: "GTA4", rating: 8} 3: {title: "GTA", rating: 9} */ // Topic: Copies of Arrays /* - previously when we sort arrays it keeps its reference. better to create a copy not to mess - with the original array - 1. Spread creates a copy of the array - 2. spread can also be used to split strings and concatenate multiple strings toggetehr - p.s concat does not change the existing array */ /* const rating = [5, 2, 4, 45, 22, 3, 1]; const spreadRating = [...rating]; // creates a copy of the array spreadRating.sort(); spreadRating.sort((a, b) => { return a - b; // ascending a-b // descending b-a }); console.log(rating); console.log(spreadRating); const name = "TheWebsiteKitchen"; const splitName = name.split(""); console.log(splitName); const singleLetters = [...name]; console.log(singleLetters); const names = ["khan", "Mohsin"]; const namesOther = ["Jon", "Snow"]; const allNames = names.concat(namesOther); console.log(allNames); const spreadALL = [...names, ...namesOther]; console.log(spreadALL); */