drop constants

Run Settings
LanguageJavaScript
Language Version
Run Command
//import require performance hooks library to use persormce const { performance } = require('perf_hooks'); // declaring array called nemo and setting first index to string 'nemo' const nemo = ['wsup', 'you ugly', 'ya momma', 'nemo']; //create new large array const large = new Array(10).fill('nemo'); // create function to look thru array and find vaue of nemo function findNemo (array){ try{ //get time now let t0 = performance.now(); console.log("time before is: " + t0); let i=0, j = array.length; //check if array is empty if(j === 0){ return; } //loop thru array to find string nemo for(; i < j; i++){ if(array[i] === 'nemo'){ console.log('Found Nemo!'); } } let t1 = performance.now(); console.log("time after is: " + t1); console.log('time took to find nemo is: ' + (t1-t0)); } catch (err) { console.err(err); throw err; } } //function call; pass in array to function findNemo(large); //0(n) -> linear time)
//declare const array const boxes = [0, 1, 2, 3, 4, 5]; //get first two boxes function logFirstTwoBoxes(array){ console.log(boxes[0]); //print value of first box // 0(1) console.log(boxes[1]); //print value of second box // 0(1) } logFirstTwoBoxes(boxes); //0(1) -> constant time
function funChallenge(input){ let a = 10; //0(1) a = 50 + 3;//0(1) let i = 0, j = input.length; for(; i < j; i++){ // 0(n) anotherFunction(); // 0(n) let stranger = true; // 0(n) a++; // 0(n) } return a; // O(1) } funChallenge(); //big O = 3 + n + n + n + n -> (3 + 4n) -> O(n)
//worst case //loop thru array to find string nemo for(; i < j; i++){ if(array[i] === 'nemo'){ console.log('Found Nemo!'); } } //nemo may be printed 1000 times so lets make it better //better case //loop thru array to find string nemo for(; i < j; i++){ if(array[i] === 'nemo'){ console.log('Found Nemo!'); break; // will exit once nemo is found } }
const array = ['ya', 'bool', 'tool', 'cool', 'school', 'mule']; function printFirstItemThenFistHalfThenSayHi100Times(items) { //print out first item in array console.log(items[0]); //O(1) //get middle index var middleIndex = Math.floor(items.length / 2); var index = 0; //loop thru each item while middle index is > curr index //print out each item at each index //then increment index by 1 while (index < middleIndex) { console.log(items[index]) index++; } //O(n/2) only logging half the items //run loop less than 100 times and print out hi each pass for (var i = 0; i < 100; i++) { console.log('hi'); } // O(100) } // O(1 + (n/2) + 100) -> O (n/2) -> O(n) printFirstItemThenFistHalfThenSayHi100Times(array);
Editor Settings
Theme
Key bindings
Full width
Lines