Exercise Solutions: Data and VariablesΒΆ

  1. Declare and assign variables

    Declare and assign a variable for each item in the list.

    1
    2
    3
    4
    5
    let shuttleName = 'Determination';
    let shuttleSpeedMph = 17500;
    let distanceToMarsKm = 225000000;
    let distanceToMoonKm = 38400;
    const milesPerKm = 0.621;
    

    Back to the exercises

  1. Calculate a space mission!

    We need to determine how many days it will take to reach Mars.

    1. Create and assign a miles to Mars variable. You can get the miles to Mars by multiplying the distance to Mars in kilometers by the miles per kilometer.

      let milesToMars = kilometersToMars * milesPerKilometer;
      
    2. Next, we need a variable to hold the hours it would take to get to Mars. To get the hours, you need to divide the miles to Mars by the shuttle's speed.

      let hoursToMars = milesToMars / shuttleSpeedMph;
      
    3. Finally, declare a variable and assign it the value of days to Mars. In order to get the days it will take to reach Mars, you need to divide the hours it will take to reach Mars by 24.

      let daysToMars = hoursToMars / 24;
      

    Back to the exercises

  1. Now calculate a trip to the Moon

    Repeat the calculations, but this time determine the number of days it would take to travel to the Moon and print to the screen a sentence that says "_____ will take ___ days to reach the Moon.".

    1
    2
    3
    4
    let milesToMoon = kilometersToMoon * milesPerKilometer;
    let hoursToMoon = milesToMoon / shuttleSpeedMph;
    let daysToMoon = hoursToMoon / 24;
    console.log(shuttleName + " will take " + daysToMoon + " days to reach the Moon.");
    

    Back to the exercises