Untitled

Run Settings
LanguageJavaScript
Language Version
Run Command
//Given 2 arrays, create a function that lets a a user know (true/false) whether // two arrays contain any common items. //Example 1: //const array1 =['a','b','c','x'] //const array2 = ['z','y','i'] //should return false //-------------------- //const array1 =['a','b','c','x'] //const array2 = ['z','y','x'] //should return true //function has 2 parameters - arrays //return true or false //runtime is O(n*m) n being input size of input1 and m being input size of input 2 //brute force approach //space complexity is O(1) const array1 =['a','b','c','x'] const array2 = ['z','y','x'] /* function containsCommonItem(arr1,arr2) { for(let i = 0; i < arr1.length; i++) { for(let j = 0; j < arr2.length; j++) { if(arr1[i] === arr2[j]) { return true; } } } return false; } console.log(containsCommonItem(array1,array2));*/ // array1 ==> obj { // a:true, // b:true, // c:true, // x:true, //} // array2[index] == obj.properties // function containsCommonItem2(arr1,arr2) { //loop through first array and create object where properties === items in array // loop through second array and check if item in second array exists on //created object. //runtime is O(n + m) //space complexity is O(n) //can we assume always 2 params let map = {}; for(let i = 0; i < arr1.length; i++) { if(!map[arr1[i]]) { const item = arr1[i]; map[item] = true; } } for(let j = 0; j < arr2.length; j++) { if(map[arr2[j]]) { return true; } } return false; } console.log(containsCommonItem2(array1,array2)); function containsCommonItem3(arr1,arr2) { return arr1.some(item => arr2.include(item)); }
//create a function that reverses a string function reverse(str) { let reverse_string = ""; for(let i = str.length -1; i > 0; i--) { reverse_string += str[i]; } return reverse_string; } const result = "Philip"; console.log(reverse(result));
Editor Settings
Theme
Key bindings
Full width
Lines