Read this over carefully and make sure you know & understand it all.
- Print output to the console
- Declaring variables
- Getting user input
- Get the length of a string
- Convert a string to a number
- Convert a string to all UPPER or lower case
- Get a specific letter from a string
- Make decisions (conditionals)
- Use the Math object
- Your Task
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.");
let final_boss;
let magic_number = 15;
let best_friend = "Mr. Squirrel";
// 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");
let length = "This is a string".length;
// or
let my_string_length = my_string.length;
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);
let my_string = "MiXed CASe STring";
let upper_string = my_string.toUpperCase();
let lower_string = my_string.toLowerCase();
// 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];
// 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;
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)
}
- 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.
- 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.
-
Convert the input to ALL UPPERCASE, just in case they enter something like "85f" or "17c".
-
Get the last character of the input and save it in a variable. This will tell us if they want Celsius or Fahrenheit.
-
Convert the input to a number using
parseInt
like this:
user_input = parseInt(user_input);
-
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!". -
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
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.