-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplicit-and-implicit-conversion-in-javascript.js
More file actions
45 lines (35 loc) · 1.63 KB
/
explicit-and-implicit-conversion-in-javascript.js
File metadata and controls
45 lines (35 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
Part 1: Debugging Challenge
The JavaScript code below contains intentional bugs related to type conversion.
Please do the following:
- Run the script to observe unexpected outputs.
- Debug and fix the errors using explicit type conversion methods like Number() , String() , or Boolean() where necessary.
- Annotate the code with comments explaining why the fix works.
Part 2: Write Your Own Examples
Write their own code that demonstrates:
- One example of implicit type conversion.
- One example of explicit type conversion.
*We encourage you to:
Include at least one edge case, like NaN, undefined, or null .
Use console.log() to clearly show the before-and-after type conversions.
*/
let result = "5" - 2;
console.log("The result is: " + result);
let isValid = false; //I removed the boolean conversion and the quotation marks from false because having a completed string was making the value true
if (isValid) {
console.log("This is valid!");
}
let age = "25";
let totalAge = Number(age) + 5; //converts the string "age" into a number so that it'll perform the additon operation correctly
console.log("Total Age: " + totalAge);
//My Implicit Type Conversion Example
let falsy = null;
if (falsy) {
console.log ("Can't Run!"); //Since null is a falsy, JS will implicitly change null to FALSE and not run the program
}
if (String(falsy)) {
console.log ("I Can Run!"); //Changed falsy to a string, giving it a true value, and outputting "I Can Run!"
}
//My Explicit Type Conversion Example
let length = "2.5 feet";
console.log(parseFloat(length)); //I am intentionally changing the string "2.5 feet" into an floating number