2

Let's say that I'm doing this because of my homework. I would like to develop some kind of schedule for the week to come (array of 6-7 elements - output result). But I have one problem. I need to figure it out how one element be positioned in the array and also his frequency must be exactly what user input is. Elements must be positioned at different index in the array.

I'm having that kind of input from user (just an example);

var arrayOfElements = ["el1","el2","el3"]; var el1Frequency = 3; var el2Frequency = 2; var el3Frequency = 1; //output array of schedule (this is just an example) var finaloutPutArray = ["el1","el2","el3","el1","el2","el1"]; 

Index of elements el1 is 0, 3 and 5, basically, I don't want elements to be repeated like this;

["el1","el1","el2","el3"...]; ["el2","el1","el1","el3"]; 

Can you please give me some ideas on how to solve this problem.

I started like this;

var finalSchedule = []; var totalDaysPerWeek = 6; for(var i =0; i < totalDaysPerWeek; i++) { ... } 
9
  • You need to loops, the first one keeps track of how many times you added your subelements, the inner one adds the elements. You might want to keep the number of repetitions in an array as welll, as this makes it easier to map them to their respective value. Commented Jun 5, 2019 at 12:29
  • Maybe try writing the algorithm for two elements first and build it up from there. Commented Jun 5, 2019 at 12:29
  • 1
    It's a bit hard to tell how you want your elements to be ordered, but from the example output, this might be as straightforward as adding all elements whos counter is above zero. Commented Jun 5, 2019 at 12:30
  • Sorting of elements highly depends of his frequencies.... Commented Jun 5, 2019 at 12:33
  • 1
    Since this appears to be homework, i think you should be trying to figure it out yourself. You will learn MUCH more if you manage to find the solution on your own. - Take a sheet of paper, work through it step by step. - I'll gladly help you in a few days, but keep on trying for a while :) Commented Jun 5, 2019 at 12:38

1 Answer 1

1

This is one pattern, check my working snippet:

var arrayOfElements = ["el1","el2","el3"]; var obj = { el1: 3, el2: 2, el3: 1}; // First determine the max recurring of an element, this will be the number of cycles fo your loop // Check key values var arr = Object.keys(obj).map(function ( key ) { return obj[key]; }); // Get max value var max = Math.max.apply( null, arr ); var finalArray = []; // Iterate from 0 to max val for(i = 0; i < max; i += 1){ // Iterate on array of elements for(k = 0; k < arrayOfElements.length; k += 1) { // If config of recurring if( obj[arrayOfElements[k]] >= i+1 ) { // Push into array finalArray.push(arrayOfElements[k]); } } } console.log(finalArray);

Sign up to request clarification or add additional context in comments.

6 Comments

I'm providing a pattern with explanation, so I don't get where the problem is.
It's not a problem of course, i appreciate your answer and the explanation, but a few years ago it was practically consense on this platform to treat homework questions a bit differently
In the linked meta, at the second condition: "Provide an answer that help user to learn". That's what I did, mate.
Yes, i know (: - I just think he would learn a lot more by getting there himself.
Yep I only stuck at this problem in my project. I also need to do website to show data etc....
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.