Arrays

Run Settings
LanguageJavaScript
Language Version
Run Command
/** * @param {number[]} nums * @return {number} */ var maxSubArray = function(nums) { // Input: nums = [4,-1,2,1,-5,4] // Output: 6 // Explanation: The subarray [4,-1,2,1] has the largest sum 6. let sum = 0; let max = nums[0] let subArray = [] for(let i = 0; i < nums.length; i++) { let innerArray = [] innerArray.push(nums[i]) for(let j = i+1; j < nums.length; j++){ innerArray.push(nums[j]) // subArray.push(nums[j]) } subArray.push(innerArray) } console.log(subArray) return sum }; console.log(maxSubArray([-2,1,-3,4,-1,2,1,-5,4]))
function reverse(str) { if (!str || str.length < 2 || typeof str !== 'string') { return "Invalid input" } const backwards = [] const totalItems = str.length - 1 for (let i = totalItems; i >= 0; i--) { backwards.push(str[i]) } return backwards.join('') } function reverse2(str) { return str.split('').reverse().join('') } const reverse3 = str => str.split('').reverse().join('') const reverse4 = str => [...str].reverse().join('') console.log(reverse4("Hello there, Mate?!"))
class MyArray { constructor() { this.length = 0 this.data = {} } get(index) { return this.data[index] } push(item) { this.data[this.length] = item this.length++ return this.length } pop() { const lastItem = this.data[this.length - 1] delete this.data[this.length - 1] this.length-- return lastItem } delete(index) { const item = this.data[index] this.shiftItems(index) } shiftItems(index) { for (let i = index; i < this.length - 1; i++) { this.data[i] = this.data[i+1] } delete this.data[this.length-1] this.length-- } }
function mergeSortedArrays(array1, array2) { const mergedArray = [] let array1Item = array1[0] let array2Item = array2[0] let i = 1 let j = 1 //check inout if (array1.length === 0) { return array2 } if (array2.length === 0) { return array1 } while (array1Item || array2Item) { if (!array2Item || array1Item < array2Item) { mergedArray.push(array1Item) array1Item = array1[i] i++ } else { mergedArray.push(array2Item) array2Item = array2[j] j++ } } return mergedArray } mergeSortedArrays([0,3,4,31], [4,6,30])
var twoSum = function(nums, target) { for(let i = 0; i < nums.length; i++) { for(let j=i+1; j < nums.length; j++) { if(nums[i] + nums[j] === target){ return [i,j] } } } };
Editor Settings
Theme
Key bindings
Full width
Lines