[CWM] Simple Operations

Overview of Code with Me Video Lectures

  • Focus on problem-solving and coding collaboratively in class.

  • Aim to integrate various topics learned throughout the course.

Creating and Organizing Code Files

  • Introduction to creating a JavaScript (JS) file using Notepad++.

  • Emphasis on file organization:

    • Suggestion to create a dedicated folder for code (e.g., "dev" located in the root of the C drive).

  • Saving files for syntax highlighting; comments convert to green indicating proper syntax.

First Problem: Calculating Distance Traveled by a Car

  • Problem statement setup:

    • Task: Write a program that calculates the distance a car travels based on its speed.

    • Given values: Speed of the car = 70 miles per hour.

  • Distance Calculation Formula:

    • Formula: Distance=Speed×Time\text{Distance} = \text{Speed} \times \text{Time}.

  • Problem requirements:

    • Inputs: Speed (70 mph), Time (6 hours, 10 hours, and 15 hours).

    • Outputs: Display distances for 6 hours, 10 hours, and 15 hours.

Breakdown of Solution Process

  1. Define inputs and respective variables:

    • var carSpeed = 70;

  2. Initialize distance variables for each time duration:

    • For 6 hours: var distanceAtSix = carSpeed * 6;

    • For 10 hours: var distanceAtTen = carSpeed * 10;

    • For 15 hours: var distanceAtFifteen = carSpeed * 15;

  3. Display results using console.log:

    • Formatted output for clarity:
      console.log("The distance after 6 hours is: " + distanceAtSix); console.log("The distance after 10 hours is: " + distanceAtTen); console.log("The distance after 15 hours is: " + distanceAtFifteen);

  4. Demonstrated method of running the code in the command prompt:

    • Access command prompt:

      • Start Menu → Search → Type "cmd."

    • Change directories to the working folder (dev):

      • Command: cd C:\dev\JS

    • Run the program using Node:

      • Command: node personal_info.js;

    • Outputs displayed for each distance calculation.

Second Problem: Calculating Miles Per Gallon (MPG)

  • Problem outline:

    • Task: Calculate a car's miles per gallon based on distances driven and gallons used.

    • Given data: Driven = 100 miles; Gallons used = 7.

  • MPG Calculation Formula:

    • Formula: MPG=Miles DrivenGallons Used\text{MPG} = \frac{\text{Miles Driven}}{\text{Gallons Used}}

    • Example: For 100 miles and 7 gallons: MPG = 100 / 7.

Setup of Variables and Outputs

  1. Define inputs as variables:

    • var milesDriven = 100;

    • var gallonsUsed = 7;

  2. Calculate MPG:

    • var mpg = milesDriven / gallonsUsed;

  3. Display MPG value:

    • Example output with console.log:
      console.log("Miles per gallon is: " + mpg);

  4. Introduction of rounding or truncating results:

    • Reference to previous lectures on using the Math object for rounding.

    • Consideration of rounding functions for specific formatting:

      • Math.floor(mpg) for rounding down to an integer.

      • Rounding for two decimal places must be revisited.

Conclusion

  • Encouragement to explore practical coding applications through these exercises.

  • Highlight on the necessity of clear understanding of distance and MPG calculations in programming.

  • Invitation to revisit topics in future videos for further clarity on JavaScript topics and coding techniques.