Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of nums.
Example 1Input: nums = [1,2,3,4]Example 2
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
var runningSum = function(nums) {# Your code here}
Answer:
var runningSum = function(nums) {// Define a new empty array and a "previous" variable to hold the last iteration's sum}
let array = [];
let previous = null;
for (let i = 0; i < nums.length; i++) {// If not the first iteration (previous is defined) append current index value + last iteration's index value to new array}
if (previous) {array.push(nums[i] + previous);} else {// If first iteration, push current index value to new array}
array.push(nums[i]);
// Replace previous' definition with current index value + last iteration's previous definition (null if this is the first loop!)
previous = nums[i] + previous;
console.log(array);
return array;
Given the array candies and the integer extraCandies, where candies[i] represents the number of candies that the ith kid has. For each kid check if there is a way to distribute extraCandies among the kids such that he or she can have the greatest number of candies among them. Notice that multiple kids can have the greatest number of candies.
Example 1Input: candies = [2,3,5,1,3], extraCandies = 3Example 2
Output: [true,true,true,false,true]
Explanation:
Kid 1 has 2 candies and if he or she receives all extra candies (3) will have 5 candies --- the greatest number of candies among the kids.
Kid 2 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids.
Kid 3 has 5 candies and this is already the greatest number of candies among the kids.
Kid 4 has 1 candy and even if he or she receives all extra candies will only have 4 candies.
Kid 5 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids.Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false]
Explanation: There is only 1 extra candy, therefore only kid 1 will have the greatest number of candies among the kids regardless of who takes the extra candy.
var kidsWithCandies = function(candies, extraCandies) {# Your code here}
Answer:
var kidsWithCandies = function(candies, extraCandies) {// Define a new empty array and a copy of the candies array so we don't modify the original}
let outputArray = [];
let candiesCopy = [...candies]];
for (let i = 0; i < candies.length; i++) {// Create a temp variable, count, to hold the current iteration's candy count}
let count = candies[i] + extraCandies;
// Sort the candiesCopy array and pass a new method so it's in descending order, this way we can check what the largest count of candies was to compare to see if the current iteration's count is greatest or not
candiesCopy.sort(function(a, b) {return b - a;});
// If our current count of candies is greater than or equal to the highest candy count in our candiesCopy array, we return true
if (count >= candiesCopy[0]) {outputArray.push(true);} else {outputArray.push(false);}
return outputArray;