Set up Html file
For these first exercises we will used the first way to build code, all in one document. So we will go to Brackets and we will write this:
The console.log() command is used to print, or log, text to the console. Consider the following example, write this within the script tags:
console.log(“Hello!”);
The alert command is used to print text on a new popup window. Consider the following example:
alert(“Hello!”);
Data Types
- Strings — Any grouping of keyboard characters (letters, spaces, numbers, or symbols) surrounded by single quotes (‘Hello’) or double quotes (“World!”). In the example above, ‘New York City’ is a string.
- Numbers — Any number with no quotations, including numbers with decimals: 4, 1516, .002, 23.42.
- Booleans — Either true or false, with no quotations. In the example above, true is a boolean.
- Null — Can only be null. It represents the absence of value.
Variables
You can save any data type in a variable. For example, here we save numbers:
const myAge = 11;
console.log(myAge);
// Output: 11
In the example above, on line 1 the myAge variable is set to 11. Below that, console.log() is used to print 11 to the console. const, short for constant, is a JavaScript keyword that creates a new variable with a value that cannot change.
Let variables however, can be reassigned.
let meal = ‘Enchiladas’;
console.log(meal);
meal = ‘Tacos’;
console.log(meal);
// output: Enchiladas
// output: Tacos
In the example above, the let keyword is used to create the meal variable with the string ‘Enchiladas’ saved to it. On line three, the meal variable is changed to store the string ‘Tacos’.
You may be wondering, when to use const vs let. In general, only use const if the value saved to a variable does not change in your program.
Prompt
Sometimes I have to get data from people, for example name or age, so I need to ask for this information through a popup window using the prompt method in the code. This information will be save in a variable
let myName = prompt (“What’s your name”);
let yourNumber = prompt (“Choose your number between 1-100”);
Libraries
We can find ore methods and resources on JavaScript math library and JavaScript documentation.
Math.floor(Math.random() * 50);
In this case:
- Math.random generates a random number between 0 and 1.
- We then multiply that number by 50, so now we have a number between 0 and 50.
- Then, Math.floor rounds the number down to the nearest whole number.
let Lottery= (Math.floor(Math.random()*100))+1;
Logical operators
JavaScript supports the following math operators:
Add: +
Subtract: –
Multiply: *
Divide: /
let x = 4;
x =x+2; // x equals 6
let x = 4;
x += 2; // x equals 6
let y = 4;
y -= 2; // y equals 2
let z = 4;
z *= 2; // z equals 8
let r = 4;
r++; // r equals 5
let t = 4;
t–; // t equals 3
There are two comparisons you might be familiar with:
- Less than: <
- Greater than: >
You may also recognize these:
- Less than or equal to: <=
- Greater than or equal to: >=
These comparisons evaluate to true or false.
- To check if two things equal each other, we write === (three = signs in a row).
- To check if two things do not equal each other, we write !== (an exclamation with two = signs in a row).
It can be confusing when to use one = sign and when to use three === signs. Use a single = to assign a value to a variable. Use ===to compare the values of two different variables.
String Interpolation
The + operator, known until now as the addition operator, is used to interpolate (insert) a string variable into a string, as follows:
let myPet = ‘armadillo’;
console.log(‘I own a pet ‘ + myPet + ‘.’);
// Output: ‘I own a pet armadillo.’
Control Flow else – if
Let’s learn how we can ask JavaScript to think like us and make decisions the way we do.
We’ll start with human-speak. Many decisions we make everyday boil down to this sentence in some form:
“If something is true, let’s do option 1, or else, if it is false, let’s do option 2.”
This sentence looks fairly similar when we write it with JavaScript. See for yourself:
let needsCoffee = true;
if (needsCoffee === true) {
console.log(‘Finding coffee’);
} else {
console.log(‘Keep on keeping on!’);
}
We can add more conditions to our if/else statement with else if. Check out how this fits into our current knowledge of if/else statements:
let stopLight = ‘green’;
if (stopLight === ‘red’) {
console.log(‘Stop’);
} else if (stopLight === ‘yellow’) {
console.log(‘Slow down’);
} else if (stopLight === ‘green’) {
console.log(‘Go!’);
} else {
console.log(‘Caution, unknown!’);
}
if (yourNumber===Lottery){
alert(myName + ” you win”);
}else{
alert(“You lose ” + myName);
}
Comments
You can write single-line comments with // and multi-line comments between /* and */.
Sé el primero en comentar