-
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
69 lines (51 loc) · 2.43 KB
/
Copy pathexplicit-and-implicit-conversion-in-javascript.js
File metadata and controls
69 lines (51 loc) · 2.43 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
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 = Boolean("false");
if (isValid) {
console.log("This is valid!");
}
let age = "25";
let totalAge = age + 5;
console.log("Total Age: " + totalAge);
*/
//Fixed code
//Part 1:
//1. Explicitly convert string 5 to number 5 substracting would have forced the conversion but this conversion gives more clarity
let result = Number("5") - 2; // 3
console.log("The result is: " + result); //The result is: 3
//2. Conversion of string "false" to boolean as Boolean("false") is truthy and will return "This is valid!" but doe to this conversion the boolean will be false and execute else statement i.e "This is not valid!"
let isValid = JSON.parse("false");
if (isValid) {
console.log("This is valid!");
} else {
console.log("This is not valid!"); // "This is not valid!"
}
// 3. when it is age = "25" the output of the totalAge will be 255 which is not the expected result instead by converting the age to number the output will be correct i.e 30
let age = "25";
let totalAge = Number(age) + 5; // 30
console.log("Total Age: " + totalAge) // Total Age: 30
// Implicit Conversion Example:
let implConvr = "10" * 2; // The multiplication operator "*" forces the string "10" to be converted to a number.
console.log("After implicit conversion: " + implConvr); // 20
// Explicit Conversion Example:
let NumberString = "123"; // valid number string
let expliNumber = Number(NumberString); // explicitly converts string to a number
console.log("Explicitly converted '123' to number:", expliNumber); // 123
// Edge Case:
let nonNumberString = "Edge Case";
let invalidConversion = Number(nonNumberString);
console.log("Converting 'Edge Case' results in:", invalidConversion); // NaN