Question

In: Computer Science

Hi, i need to generate into groups the array.For example: ([ 'a', 'b', 'c', 'd' ],...

Hi, i need to generate into groups the array.For example:

([ 'a', 'b', 'c', 'd' ], 2)=>[ [ 'a', 'b' ], [ 'c', 'd' ] ]

([ { id: 5 }, { id: 10 }, { id: 20 } ], 2)=>. ([ [ { id: 5 }, { id: 10 } ], [ { id: 20 } ] ])

In javascript please

utils={ }

utils.generateGroups = () => {
// INPUT arguments
// - A 1-dimensional array
// - The length of each subgroup that should be created
//
// RETURN value
// - A 2-dimensional array of arrays. Each subarray should be as long as the length argument passed in to the function, except for the final subarray, which can be shorter and contain a "remainder" smaller than that length.
//
//your code here
};

Solutions

Expert Solution

Code

utils = {}

utils.generateGroups = (arr, size) => {

// INPUT arguments

// - A 1-dimensional array

// - The length of each subgroup that should be created

//

// RETURN value

// - A 2-dimensional array of arrays. Each subarray should be as long as the length argument passed in to the function, except for the final subarray, which can be shorter and contain a "remainder" smaller than that length.

//

let newarr = [], finalArr = []

for (let i = 0; i < arr.length; i++) {

if (newarr.length < size) {

newarr.push(arr[i])

}

else {

finalArr.push(newarr)

newarr = [arr[i]]

}

}

finalArr.push(newarr)

return finalArr

};

console.log(utils.generateGroups(['a', 'b', 'c', 'd'], 2))

console.log(utils.generateGroups([ { id: 5 }, { id: 10 }, { id: 20 } ], 2))

Screenshot

Output


Related Solutions

Hi ,I need your answer today for all parts /a/b/c/d of these question below.Br/Ha This question...
Hi ,I need your answer today for all parts /a/b/c/d of these question below.Br/Ha This question is about the wage distribution. a) The Gini-coefficient can be used to measure wage inequality. How is this measure formally defined? Also provide a graphical illustration. b) The P90/P10 and P50/P10 are also common measures of wage inequality. Explain the measures. c) What are the pros and cons with the two types of measures in a) and b)? Are there situations where one of...
Hi ,I need your answer today for all parts /a/b/c/d of these question below.Br/Ha This question...
Hi ,I need your answer today for all parts /a/b/c/d of these question below.Br/Ha This question is about human capital accumulation in terms of on-the-job training. a) Define general and firm specific human capital. b) Discuss who pays for investments in human capital that leads to general human capital and to firm specific human capital.
I have A B and C answered I only need the answer to D and E....
I have A B and C answered I only need the answer to D and E. I have included the correct answers for A B and C Problem 8-41 (LO. 2, 3, 9) Lori, who is single, purchased 5-year class property for $200,000 and 7-year class property for $400,000 on May 20, 2016. Lori expects the taxable income derived from her business (without regard to the amount expensed under § 179) to be about $800,000. Lori wants to elect immediate...
If I show (A and (B → C)) → D and (A and (C → B))...
If I show (A and (B → C)) → D and (A and (C → B)) → D, can I conclude A → D?
I NEED ANSWER OF A, B, C, D You are probably familiar with (and may have...
I NEED ANSWER OF A, B, C, D You are probably familiar with (and may have used) back belts, which are widely used by workers to protect their lower backs from injuries caused by lifting. A study was conducted to determine the usefulness of this protective gear. Here is a partial description of the study, published in the Journal of the American Medical Association and reported by the Associated Press (December 5, 2000): New research suggests that back belts, which are widely...
I need to know the steps of solution for part (b,c,d) I have been trying for...
I need to know the steps of solution for part (b,c,d) I have been trying for almost 2 days. A 195 -keV X-ray photon coherently scatters off one of the valence electrons of a nitrogen atom. Assume that the scattering angle of the photon is ? = 15
please i need the solve for A&B&C&D&E ESPECIALLY B&C&D&E Company Information Wood Work Ltd manufacture specialist...
please i need the solve for A&B&C&D&E ESPECIALLY B&C&D&E Company Information Wood Work Ltd manufacture specialist wood furniture and sell their products all over Saudi  Arabia. The company was established three  years ago in Jeddah and is performing well to date. Wood work Ltd have three main product lines; TV tables, dining table and chairs. The following financial information has been provided.   Financial Information TV tables dining Tables Chairs Selling Price per unit SAR 1,000 SAR 5,000 SAR 700 Direct Materials (wood...
Hi I would like to see an example in c++ of Stack As Linked List I...
Hi I would like to see an example in c++ of Stack As Linked List I need a seek() function which receives a double number X and which returns in which position in the stack is X. If the value X is not in the stack, the function should return -1
create a decision tree for a,b,c,d what do u need? a,b,c,d are the elements that needs...
create a decision tree for a,b,c,d what do u need? a,b,c,d are the elements that needs to be sorted
In Unity with C#, I need to implement a dice simulator in my character generator(D&D). I...
In Unity with C#, I need to implement a dice simulator in my character generator(D&D). I have 6 sides of UI images(2D). With my code, I am able to roll and get the number in my console text(randomly). But, I need help for rolling 5 dices, not the 1 dice. Also, I need to add all the three highest rolls with 5 dices and get a result in my display. Ex) 1. Players must roll 5d6 and add the three...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT