0

I wanted to generate mock data with the below structure.

Conditions,

Start time and end time interval should be 30 Mins or 1 Hr.

2-3 mocks for the same day with different time intervals

  [{
    Id: 1,
    Subject: 'Bob',
    StartTime: new Date(2020, 6, 11, 9, 30),
    EndTime: new Date(2020, 6, 11, 11, 0),
    CategoryColor: '#1aaa55'
  }]

Below is code I've written to generate my mock data,

But I'm not sure how to generate data logic. Please help

function random() {
  for (let index = 0; index <= 100; index++) {
    let x = {
      Id: getRandomInt(1, 1000),
      Subject: generateName(),
      StartTime: new Date(2020, 6, 11, 9, 30),
      EndTime: new Date(2020, 6, 11, 11, 0),
      CategoryColor: getRandomColor()
    }
    data.push(x);
  }
}
2
  • The interval should be exactly 30min and 1hr or any value between this values ? Commented May 27, 2020 at 9:47
  • 30 mins is preferred Commented May 27, 2020 at 9:52

1 Answer 1

0

You can use this for a random name generator. For a random number generator you can use Math.random() (which always returns a number between 0-1) in combination with Math.floor(). For example:

Math.floor(Math.random() * 101);  // returns a random integer from 0 to 100

For date generation see this answer

If I include the interval it should look like this:

let generated_data = []; // this will be the final data
let colors = ["Blue", "Red", "Green"]; // define all the colors you need


function getRandomInt(min, max) { // generate random number
    return Math.floor(Math.random() * (max - min)) + min;
}

function capFirst(string) { // make the first letter capital letter
    return string.charAt(0).toUpperCase() + string.slice(1);
}


function generateName(){ //generates random Name
    var name1 = ["abandoned","able","absolute","adorable"]; // you can add more elements in this list, if you want a wider choice
    var name2 = ["people","history","way","art","world"]; // you can add more elements in this list, if you want a wider choice
    var name = capFirst(name1[getRandomInt(0, name1.length)]) + ' ' + capFirst(name2[getRandomInt(0, name2.length + 1)]);
    return name;
}


function randomDate(date1, date2){ // generates random date
    var date1 = date1 || '01-01-1970'
    var date2 = date2 || new Date().toLocaleDateString()
    date1 = new Date(date1).getTime()
    date2 = new Date(date2).getTime()
    if( date1>date2){
            return new Date(getRandomInt(date2,date1)).toLocaleDateString()   
    } else{
        return new Date(getRandomInt(date1, date2)).toLocaleDateString()  
    }
}

function generateRandomObject() { // adds a random object in the objects list
    let obj = {
        Id: getRandomInt(1, 1000),
        Subject: generateName(),
        StartTime: randomDate('01/01/2000', '01/01/2020'),
        EndTime: randomDate('01/01/2000', '01/01/2020'),
        CategoryColor: colors[getRandomInt(0, colors.length)]
    }
    generated_data.push(obj);
}


setInterval(generateRandomObject, 30 * 60 * 1000); // sets an interval of 30 mins in which the function  will be executed
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.