Skip to content

Instantly share code, notes, and snippets.

@MisterBrash
Last active March 18, 2024 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MisterBrash/c727cd1e905bd4758c2b8915bbacd25a to your computer and use it in GitHub Desktop.
Save MisterBrash/c727cd1e905bd4758c2b8915bbacd25a to your computer and use it in GitHub Desktop.
ICS3U Mid-Unit Review for Unit 1

What have we learned so far?

Read this over carefully and make sure you know & understand it all.

Table of Contents

Print output to the console

console.log();  
console.log("You can print strings");
console.log("You can print numbers:", x);
console.log("You can add tabs \t and \n new lines.");

Declaring Variables

let final_boss; 
let magic_number = 15;
let best_friend = "Mr. Squirrel"; 

Getting user input

// We need to declare the prompt function
const prompt = require("prompt-sync")();
// Then we can use it to get a STRING from standard input
let user_input = prompt("Some message here"); 

Get the length of a string

let length = "This is a string".length;
// or
let my_string_length = my_string.length;

Convert a string to a number

let str = "76X";
str = Number(str);   // NaN - Not a Number
str = parseInt(str); // 76, it stops when it hits a character

// Another example (these both work)
let fake_number = "6";
fake_number = Number(fake_number);
fake_number = parseInt(fake_number);  

Convert a string to all UPPER or lower case

let my_string = "MiXed CASe STring";
let upper_string = my_string.toUpperCase();
let lower_string = my_string.toLowerCase();

Get a specific letter from a string

// Using charAt()
let first_char = my_string.charAt(0);
// Using square-bracket indexes
let first_letter = my_string[0];
let last_letter = my_string[my_string.length - 1];

Use the Math object

// Round to the nearest whole number
let rounded = Math.round(5.76);
// Round to one decimal
rounded = Math.round(5.76 * 10)/10;

// Get a random decimal number
let rnd = Math.random();

// Get PI to 9 decimals
let pi = Math.PI;

Make decisions (conditionals)

if (user_input == 20) {
  // Do some code
} else if (user_input > 20) {
  // Do something else
} else {
  // All other cases (less than 20 in this example)
}

Your Task

  • Create the coding file Review 1.1-1.5.js
  • You will be using prompt(), so declare it at the top of your code file.
  1. Ask the user to enter a temperature, it should read like this:
Enter a temperature like 30C or 120F:

The user will enter a number, followed by a single letter - either C or F. Careful - they might enter lowercase letters!
For Example:

Enter a temperature like 30C or 120F: 68f

In that example, the user wants to know what 69F is in Celsius.

  1. Convert the input to ALL UPPERCASE, just in case they enter something like "85f" or "17c".

  2. Get the last character of the input and save it in a variable. This will tell us if they want Celsius or Fahrenheit.

  3. Convert the input to a number using parseInt like this:

user_input = parseInt(user_input);
  1. If the last letter was "C", convert their number to Fahrenheit. If the last letter was "F", convert their number to Celsius. Round answers to one decimal place.
    If the last letter was something other than "C" or "F", output "Error!".

  2. Output the converted value as a temperature string like "45C" or "127F". Here's two examples of the program being used:

Enter a temperature like 30C or 120F: 24c
75.2F
Enter a temperature like 30C or 120F: 124F
51.1C

What do I do when I'm done?

The next thing we will be looking at is Logical Operators. More specifically, we'll be looking at combining conditions in an if-statement by using AND (&&) and OR (||). Feel free to look those up and play around with your own code to see how they work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment