Quick actions

cmd+k|ctrl+k

Navigation

Languages

Data Structures & Algorithm

Snippet info

Language

JavaScript

Visibility

public

Author

jack4ssy

Created

2024-10-27T15:29:52.288964Z

Updated

2024-11-26T01:10:08.093878Z

// Eps 1 - What is good code?

// const nemo = ['Nemo'];

// function findNemo(array) { 
//     for (let i = 0; i < array.length; i++) {
//         if (array[i] == 'Nemo') {
//             console.log("Found Nemo");
//         }
//     }
// }

// findNemo(nemo);

// Eps 2 - Big O & Scalability

// const animal = ['macan'];
// const everyone = ['ucup', 'otong', 'dudung', 'macan', 'badak', 'jerapah', 'serigala', 'kerbau', 'sapi', 'kambing'];
// const large = new Array(1000).fill('macan');

// function findMacan(arr) {
//     let t0 = performance.now();
//     for(let i = 0; i < arr.length; i++) {
//         if(arr[i] == 'macan') {
//             console.log('Found Macan!');
//         }
//     }
//     let t1 = performance.now();
//     console.log('call to find macan took ' + (t1 - t0) + ' milliseconds');
// }

// findMacan(large);

// Eps 3 - O(n)

// const animal = ['macan'];
// const everyone = ['ucup', 'otong', 'dudung', 'macan', 'badak', 'jerapah', 'serigala', 'kerbau', 'sapi', 'kambing'];
// const large = new Array(1000).fill('macan');

// function findMacan(arr) {
//     for(let i = 0; i < arr.length; i++) {
//         if(arr[i] == 'macan') {
//             console.log('Found macan!');
//         }
//     }
// }

// findMacan(large); // O(n) <-- Linear Time

// Eps 4 - O(1)

// const boxes = [1,2,3,4,5,6,7,8,9];

// function logFirstTwoBoxes(boxes) {
//     console.log(boxes[3]);
//     console.log(boxes[6]);
// }

// logFirstTwoBoxes(boxes);

// Eps 5 - Exercise: Big O calculation 1

// What is the Big O of the below function?(Hint, you may want to go line by line)
// function funChallange(input) {
//     let a = 10; // O(1) <-- because this is only running once whe we run funchallange
//     a = 50 + 10; // O(1) 
    
//     for(let i = 0; i < input.length; i++) { // O(n) <-- loops are linear time
//         anotherfunction(); // O(n) <-- calling to another function outside of the funChallange
//         let stranger = true; // O(n) <-- this runs as many times as this loop happens
//         a++; // O(n) <-- this once again runs o of n times
//     }
//     return a; // O(1) <-- this runs just once every time funChallange gets run
// }

// Big O(3 + 4n)

// Eps 6 - Exercise: Big O calculation 2

// What is the Big O of the below function? (Hint, you may want to go line by line)
// function anotherFunChallenge(input) {
//     let a = 5; // O(1) <-- assigning variables
//     let b = 10; // O(1) <-- assigning variables
//     let c = 50; // O(1) <-- assigning variables
//     for (let i = 0; i < input; i++) { // O(n) <-- loops are linear time
//       let x = i + 1; // O(n) <-- running based on the input
//       let y = i + 2; // O(n) <-- running based on the input
//       let z = i + 3; // O(n) <-- running based on the input
//     }
//     for (let j = 0; j < input; j++) { // O(n)
//       let p = j * 2; // O(n) <-- running based on the input
//       let q = j * 2; // O(n) <-- running based on the input 
//     }
//     let whoAmI = "I don't know"; // O(1) <-- just run once
//   }
// Big O = 4 + 7n = O(n)

// Eps 7 - Big O Rule 1: Worst Case

// const everyone = ['munir', 'nemo', 'frank', 'sappy', 'gajah', 'kambing', 'tikus', 'kucing', 'anjing', 'marmut'];

// function findKucing(arr) {
//     for(let i = 0; i < arr.length; i++) {
//         console.log('running');
//         if(arr[i] == 'kucing') {
//             console.log('Found Kucing!');
//             break;
//         }
//     }
// }

// findKucing(everyone);

// Eps 8 - Big O Rule 2: Remove Constants

// function printFirstItemThenFirstHalfThenSayHi100Times(items) {
//     console.log(items[0]);
    
//     var middleIndex = Math.floor(items.length / 2);
//     var index = 0;
    
//     while(index < middleIndex) {
//         console.log(items[index]);
//         index++;
//     }
    
//     for(var i = 0; i < 100; i++) {
//         console.log('hi');
//     }
// }

// Eps 9 - Big O Rule 3: Different Terms For Inputs

// function compressTwoBoxes(boxes, boxes2) {
//     boxes.forEach(function(boxes) {
//         console.log(boxes);
//     });
    
//     boxes2.forEach(function(boxes) {
//         console.log(boxes);
//     });
// }

// O(a + b);

// Eps 10 - O(n^2)

// const boxes = ['a', 'b', 'c', 'd', 'e'];

// function logAllPairsOfArray(arr) {
//     for(let i = 0; i < arr.length; i++) {
//         for(let j = 0; j < arr.length; j++) {
//             console.log(arr[i], arr[j]);
//         }
//     }
// }

// logAllPairsOfArray(boxes);
// O(i * j);

// Eps 11 - Big O Rule 4: Drop Non Dominants

// function printAllNumbersThenAllPairSums(numbers) {
    
//     console.log('These are the numbers: ');
//     numbers.forEach(function(number) {
//         console.log(number);
//     });
    
//     console.log('And these are their sums: ');
//     numbers.forEach(function(firstNumber) {
//         numbers.forEach(function(secondNumber) {
//             console.log(firstNumber + secondNumber);
//         });
//     });
// }

// printAllNumbersThenAllPairSums([1,2,3,4,5]);
// O(n + n^2) = O(n^2)

// Eps 12 - Exercise: Space Complexity

// function boooo(n) {
//     for(let i = 0; i < n.length; i++) {
//         console.log('BOOO');
//     }
// }

// boooo([1,2,3,4,5]); // Space Complexity: O(1)

// function arrayOfHiNTimes(n) {
//     let hiArray = [];
//     for(let i = 0; i < n; i++) {
//         hiArray[i] = 'hi';
//     }
//     return hiArray;
// }

// console.log(arrayOfHiNTimes(6)); // Space Complexity: O(n)

// Eps 13 - Javascript loops

// const animal = ['macan'];
// const everyone = ['ucup', 'otong', 'dudung', 'macan', 'badak', 'jerapah', 'serigala', 'kerbau', 'sapi', 'kambing'];
// const large = new Array(1000).fill('macan');


// const findMacan2 = array => {
//     array.forEach(hewan => {
//         if(hewan === 'macan') {
//             console.log('Found Macan');
//         }
//     });
// };

// const findMacan3 = array => {
//     for (let hewan of array) {
//         if (hewan === 'macan') {
//             console.log('Found Macan');
//         }
//     }
// };

// findMacan2(everyone);
// findMacan3(everyone);











INFO