@@ -791,4 +791,126 @@ https://claude.ai/public/artifacts/29027752-4a20-451f-a5a0-743050848a4b
7917913.call back function a function that passed trhough argument fir another function
792792
793793
794- here call back function passed as argument to huigher order function its abiloity is called first class function
794+ here call back function passed as argument to huigher order function its abiloity is called first class function
795+
796+
797+ Day-8:
798+
799+ // Sample array
800+ let arr = [5, 1, 3, 6, 2];
801+
802+ // ========================== MAP ==========================
803+ console.log("================= MAP =================");
804+ // MAP: Creates a new array by applying a function to each element
805+
806+ // Method 1: Named function declaration outside map
807+ function double(i) {
808+ return i * 2;
809+ }
810+ console.log("Double:", arr.map(double));
811+
812+ // Method 2: Anonymous function inside map
813+ console.log("Triple:", arr.map(function(i) {
814+ return i * 3;
815+ }));
816+
817+ // Method 3: Arrow function (most concise)
818+ console.log("Binary:", arr.map(i => i.toString(2)));
819+
820+
821+ // ========================== FILTER ==========================
822+ console.log("\n ================= FILTER =================");
823+ // FILTER: Creates a new array with elements that pass a test
824+
825+ // Method 1: Named function declaration
826+ function isOdd(i) {
827+ return i % 2; // Non-zero values are truthy (odd numbers)
828+ }
829+ console.log("Odd numbers:", arr.filter(isOdd));
830+
831+ // Method 2: Anonymous function
832+ console.log("Even numbers:", arr.filter(function(i) {
833+ return i % 2 === 0;
834+ }));
835+
836+ // Method 3: Arrow function
837+ console.log("Odd numbers (arrow):", arr.filter(i => i % 2 !== 0));
838+
839+
840+ // ========================== REDUCE ==========================
841+ console.log("\n ================= REDUCE =================");
842+ // REDUCE: Accumulates array values into a single result
843+
844+ // Method 1: Named function declaration
845+ function sum(acc, curr) {
846+ acc += curr;
847+ return acc;
848+ }
849+ console.log("Sum:", arr.reduce(sum, 0));
850+
851+ // Method 2: Anonymous function
852+ console.log("Sum (anonymous):", arr.reduce(function(acc, curr) {
853+ acc += curr;
854+ return acc;
855+ }, 0));
856+
857+ // Method 3: Finding maximum value
858+ console.log("Max value:", arr.reduce(function(acc, curr) {
859+ if (acc < curr) {
860+ acc = curr;
861+ }
862+ return acc;
863+ }, 0));
864+
865+ // Method 4: Max with arrow function
866+ console.log("Max (arrow):", arr.reduce((acc, curr) => {
867+ if (acc < curr) {
868+ acc = curr;
869+ }
870+ return acc;
871+ }, 0));
872+
873+
874+ // ========================== PRACTICAL EXAMPLES ==========================
875+ console.log("\n ================= PRACTICAL EXAMPLES =================");
876+
877+ // Array of objects
878+ let people = [
879+ {first: "Swathi", lastName: "Amaravadi", age: "27"},
880+ {first: "Rajesh", lastName: "Kodakandla", age: "33"},
881+ {first: "Gopireddy", lastName: "Amaravadi", age: "25"},
882+ {first: "Anand", lastName: "Yannamaneni", age: "27"}
883+ ];
884+
885+ // Example 1: Map - Create full names array
886+ let fullNames = people.map(person => person.first + " " + person.lastName);
887+ console.log("Full names:", fullNames);
888+
889+ // Example 2: Reduce - Count occurrences of each age
890+ let ageFrequency = people.reduce((acc, curr) => {
891+ if (acc[curr.age]) {
892+ acc[curr.age]++;
893+ } else {
894+ acc[curr.age] = 1;
895+ }
896+ return acc;
897+ }, {});
898+ console.log("Age frequency:", ageFrequency);
899+
900+ // Example 3: Chaining methods - Get lastNames of people over 30
901+ let over30LastNames = people
902+ .filter(person => person.age > 30)
903+ .map(person => person.lastName);
904+ console.log("LastNames of people over 30:", over30LastNames);
905+
906+ // Example 4: Alternative using reduce for the same operation
907+ let over30LastNamesAlt = people.reduce((acc, curr) => {
908+ if (curr.age > 30) {
909+ acc.push(curr.lastName);
910+ }
911+ return acc;
912+ }, []);
913+ console.log("LastNames of people over 30 (with reduce):", over30LastNamesAlt);
914+
915+
916+ https://claude.ai/public/artifacts/864c8182-bd9e-4d29-827a-2e21653dbf10
0 commit comments