Lesson 10 - Activity 2: Do you need help creating control flow?

Time: 1 - 5 hours

Purpose: This activity helps you get familiar with creating if statements and loops by tasking you with using them in the random data generator.

Introduction: We want to be able to create lots of different test data quickly that doesn’t require a lot of copy and pasting as well as put some logic into our codebase around the total price random generator. So for this activity we’re going to use if statements and loops.

For this activity, you can either use your existing random data generator code you’ve created or you can use this example code-base.

Activity: Using loops, update your current random data generator code-base to:

  1. Call the data creation function in a loop
  2. Update the random price function with a loop and an if statement to make sure it returns an integer above 100

If you need help, post questions on this thread for support.

loving the course so far…

I have a question here…

when updating the price function, do we NEED to include an IF statement? for this one I have included a WHILE number <= 100 then increment if under 100. which seems to fulfil the criteria.

I’m struggling to figure how to just run the function again IF the result is <= 100 but hurting my brain haha

Hi @jamie6bly

I think you’re almost there but just to be clear, can you share your code it will help me in my feedback.

Thanks,
Mark

thanks @mwinteringham so far I have this…

const pickRandomPrice = () => {

let randomValue = Math.random()
let totalRoomPrice = Math.floor(randomValue * 300);
while(totalRoomPrice <= 100){
totalRoomPrice++
}
return totalRoomPrice

}

the trouble is with this is that everything below 100 on the initial function, then becomes 101. I assuming the purpose of the activity is to just run the function again but maybe I’m over complicating it!

Hi @jamie6bly

You’re correct. You basically want to keep looping until your totalRoomPrice is less than 100. So rather than look at adding or subtracting from totalRoomPrice you could simply keep creating new values for totalRoomPrice until it matches the criteria you want.