Challenge 1
#1
In the dev tools console, figure out how to do the following:
Create a boolean variable called myBoolean and set it to true.
const myBoolean = true;
Create a string variable called myString and set it to hello world.
const myString = 'hello world';
Create a number variable called firstNumber and set it equal to 20.
const firstNumber = 20;
Create another number variable called secondNumber and set it equal to 40.
let secondNumber = 40;
Re-assign secondNumber and set it equal to 80.
secondNumber = 80;
Create an array called myarray and put myBoolean at index 0, and myString at index 1.
const myArray = [ myBoolean, myString ];
Create an object called myObject and assign myArray to a property called firstProperty, and the sum of firstNumber and secondNumber to a property called sumProperty.
const myObject = {
firstProperty: myArray,
sumProperty: firstNumber + secondNumber
};
Print myObject to the console.
console.log(myObject);
Print the sumProperty of myObject to the console.
console.log(myObject.sumProperty);
Print the value at index 1 of firstProperty.
console.log(myObject.firstProperty[1]);
#2
There are three things wrong with this code. Find them and explain why they are wrong.
const some Number = 20;
someNumber = 50
- The variable is invalid. The name of the variable can't be seperated with space. So it should be
someNumber, notsome Number. - After using
constto assign a value, that value can't be changed. If we want to re-assign the value of the variable, we should uselet. - The line
someNumber = 50is missing a semi-colon at the end.
#3
What does resultVariable equal? What data type is it?
const variable1 = 20;
const variable2 = '40';
const resultVariable = variable1 + variable2;
console.log(resultVariable);
The resultVariable will print "2040", and it is a string.
You can verify this by executing the following code in the console:
// This will return "string", which tells us that the variable is a string data type
typeof resultVariable
The reason for this is because JavaScript is a "weakly typed" language. In this case, we are trying to add a number and a string together. In some programming languages like Python which are "strongly typed", you would get an error trying to do this. But since JavaScript is "weakly typed", it allows you to do operations with multiple variable types at once.
This leads us to the second part of this solution. Since JavaScript allows you to add a number and a string together, you might ask–why does it end up as a string rather than a number?
This is where the concept of "implicit type coercion" comes in. Since we are not explicitly declaring each variable's type (static typing), JavaScript has to figure it out when the code runs. In this case, when you add a string and a number together, it automatically becomes a string, and that is just a rule of JavaScript.
Imagine what would happen with this code:
const stringVariable = 'some value here';
const numberVariable = 20;
console.log(stringVariable + numberVariable);
Here, it wouldn't make much sense to convert the result to a number because you can't add these two values! In our first example, you could have converted variable2 into a number and then added them together, but here, you can't convert 'some value here' into a number.
Here's how we could have turned the original result into a number:
const variable1 = 20;
const variable2 = '40';
const resultVariable = variable1 + Number(variable2);
console.log(resultVariable); // Prints 60, and is a number
By using JavaScript's built-in Number() function, we can convert a string to a number
#4
Why is this code invalid? Edit this until it's valid.
const objectVariable = {
property1: 'i am property 1';
property2: 'i am property 2';
property3: [20, 30, 40];
};
console.log(objectVariable.property3[2])
The code above is invalid because it contains semi-colons ; in the wrong places.
When creating objects, you need to place a comma after each object, NOT a semi-colon.
Here is the fixed program that will properly run in your Console.
const objectVariable = {
property1: 'i am property 1',
property2: 'i am property 2',
property3: [20, 30, 40]
};
console.log(objectVariable.property3[2]);
#5
Why does this code not work? Edit until it works.
const myArray = [20, 30, 40];
console.log(myArray[3]);
Remember, arrays start their indexing at 0, so this array has the following index values:
// Index 0 1 2
const myArray = [20, 30, 40]
And therefore, this is the correct way to write this program.
const myArray = [20, 30, 40];
console.log(myArray[2]);