Skip to content

Commit c457c48

Browse files
committed
committing my changes
1 parent ce92942 commit c457c48

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

explicit-and-implicit-conversion-in-javascript.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,33 @@ Use console.log() to clearly show the before-and-after type conversions.
2121

2222
let result = "5" - 2;
2323
console.log("The result is: " + result);
24+
//I don't think there's an error in this first block? Javascript coerces the string "5" into a number, and the output for this block when I first ran this file in my terminal was "The result is: 3". Unless we want it to print out a number that is not mathematically correct, I'm not seeing an error here.
2425

25-
let isValid = Boolean("false");
26+
let isValid = Boolean(false);
2627
if (isValid) {
2728
console.log("This is valid!");
2829
}
30+
else
31+
console.log("false");
32+
//This block originally returned "This is valid!" because the value "false" in the parentheses next to the Boolean function was a string. Javascript read this as a truthy value, and so it printed out the string in the console.log. To debug this, I removed the parentheses in "false". I added an "else" fuction to print out false to have the terminal explicity state that the variable was a false bool.
2933

3034
let age = "25";
35+
age = Number(age);
3136
let totalAge = age + 5;
3237
console.log("Total Age: " + totalAge);
38+
// To stop the program from string concantenating the variable "age" with the number it was adding to it in the third line (third line after I added my correction), I used the Number() operator to convert the value type to a number, which could then get added in correctly in the variable "totalAge"
39+
40+
41+
42+
//My explicit example
43+
44+
let students = "18";
45+
let teachers = 4;
46+
students = Number(students);
47+
let allAttendees = students + teachers;
48+
console.log(allAttendees);
49+
50+
//My implicit example
51+
52+
let pizzas = 3;
53+
console.log(`You have ordered ${pizzas} pizzas today`);

0 commit comments

Comments
 (0)