SunJet Liu

var prystring = function(char, num){
let string = '\n'
for (let i = 1 ; i <= num; i++){
for(let j = num; j > i; j--){
string += " "
}
for(let k = 0; k < i * 2 -1; k++){
string += `${char}`
}
for(let j = num; j > i; j--){
string += " "
}
string += "\n"
}
console.log(string)
}

prystring('*',5)
'
*
***
*****
*******
*********
'
var strings = function(char, num){
let string = '\n'
for (let i = 0 ; i < num; i++){
for(let j = 0; j < i; j++){
string += " "
}
for(let k = 0; k < 2 * (num-i) -1; k++){
string += `${char}`
}
string += "\n"
}
console.log(string)
}

strings('*',5)
'
*********
*******
*****
***
*
'

--

--

/*
Simple String Sort Function
Given two arguments, a string of characters (input) and a sort string (sortStr),
implement a simple sort function that returns the characters to sort in the order
specified by the sort string. If any characters do not appear in the sort string,
they go at…

--

--

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example 1:

Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Example 2:

Input: nums = [1]
Output: 1

Example 3:

Input: nums = [5,4,-1,7,8]
Output: 23

Solution:

var maxSubArray = function(nums) {
let sum = 0;
let maxSum = -Infinity;

if(nums.length === 0) return 0;
if(nums.length === 1) return nums[0]

for(let i = 0;i<nums.length;i++){
sum+=nums[i];
maxSum = Math.max(maxSum,sum);
if(sum < 0) sum = 0;
}
return maxSum;
};

--

--

Write a function, intersection, that takes in two arrays, a,b, as arguments. The function should return a new array containing elements that are in both of the two arrays.

You may assume that each input array does not contain duplicate elements.

const intersection = (a, b) => {
// todo
let set = new Set()
let arr = []
for( let i = 0; i < a.length; i++){
set.add(a[i])
}
for(let j = 0; j < b.length; j ++){
if (set.has(b[j])){
arr.push(b[j])
}
}
return arr
}

--

--