|
1 | 1 | /*
|
2 | 2 |
|
3 |
| -Part 1: Debugging Challenge |
4 |
| -The JavaScript code below contains intentional bugs related to type conversion. |
5 |
| -Please do the following: |
6 |
| - - Run the script to observe unexpected outputs. |
7 |
| - - Debug and fix the errors using explicit type conversion methods like Number() , String() , or Boolean() where necessary. |
8 |
| - - Annotate the code with comments explaining why the fix works. |
9 |
| -
|
10 |
| -Part 2: Write Your Own Examples |
11 |
| -Write their own code that demonstrates: |
12 |
| - - One example of implicit type conversion. |
13 |
| - - One example of explicit type conversion. |
14 |
| -
|
15 |
| - *We encourage you to: |
16 |
| -Include at least one edge case, like NaN, undefined, or null . |
17 |
| -Use console.log() to clearly show the before-and-after type conversions. |
| 3 | +Debugging Type Conversions: |
| 4 | +
|
| 5 | +After running the given code, the output shows: |
| 6 | +
|
| 7 | +The result is: 3 |
| 8 | +This is valid! |
| 9 | +Total Age: 255 |
| 10 | +
|
| 11 | +Fix 1- |
| 12 | +I'm not certain exactly what there is to fix on this one, seeing as it printed: |
| 13 | +"The result is: 3" |
| 14 | +Which I would've expected. I'm not sure what other outcome was trying to be achieved here. The "5" is a string, and with my understanding of implicit conversion, |
| 15 | +I was under the understanding that for subraction, Javascript converts strings to numbers. Therefore it would be 5-2=3. |
| 16 | +
|
| 17 | +I suppose if you wanted to be explicit in your coding, or if for whatever reason you received a different result, you could: |
| 18 | +let result = Number("5") - 2; |
| 19 | +console.log("The result is: " + result); // Output: The result is: 3 (here we explicity converted 5 to a number) |
| 20 | +
|
| 21 | +Fix 2- |
| 22 | +Boolean("false") returns true because it's a non-empty string |
| 23 | +// Correct approach: Use a strict comparison to "false" string |
| 24 | +let isValid = Boolean("false"); // Still true because "false" is a non-empty string |
| 25 | +
|
| 26 | +let revisedIsValid = ("false" === "true"); |
| 27 | +if (revisedIsValid) { |
| 28 | + console.log("This is valid!"); // This will not print |
| 29 | +} |
| 30 | +
|
| 31 | +Fix 3- |
| 32 | +This piece of code is concatanating 25 and 5 because its turning 5 into a string and mushing them together instead of adding the sum. |
| 33 | +We'll need to convert 'age' to a number before addition to avoid this. |
| 34 | +
|
| 35 | +let age = "25"; |
| 36 | +let totalAge = Number(age) + 5; |
| 37 | +console.log("Total Age: " + totalAge); // Output: Total Age: 30 |
| 38 | +
|
| 39 | +Examples of Type Conversion- |
| 40 | +
|
| 41 | +Implicit Type Conversion |
| 42 | +let implicitExample = "5" * 2; // "5" is implicitly converted to a number |
| 43 | +console.log(implicitExample); // Output: 10 |
| 44 | +
|
| 45 | +Explicit Type Conversion |
| 46 | +let explicitExample = Number("10"); // Explicitly converting string to number |
| 47 | +console.log(explicitExample); // Output: 10 |
| 48 | +
|
| 49 | +Edge Case: Converting NaN |
| 50 | +let edgeCaseNaN = Number("Hello"); // This results in NaN |
| 51 | +console.log(edgeCaseNaN); // Output: NaN |
18 | 52 |
|
19 | 53 | */
|
20 | 54 |
|
21 | 55 |
|
22 |
| -let result = "5" - 2; |
| 56 | +let result = Number("5") - 2; |
23 | 57 | console.log("The result is: " + result);
|
24 | 58 |
|
25 |
| -let isValid = Boolean("false"); |
26 |
| -if (isValid) { |
| 59 | +let revisedIsValid = ("false" === "true"); |
| 60 | +if (revisedIsValid) { |
27 | 61 | console.log("This is valid!");
|
28 | 62 | }
|
29 | 63 |
|
30 | 64 | let age = "25";
|
31 |
| -let totalAge = age + 5; |
| 65 | +let totalAge = Number(age) + 5; |
32 | 66 | console.log("Total Age: " + totalAge);
|
| 67 | + |
| 68 | +let implicitExample = "5" * 2; |
| 69 | +console.log(implicitExample); |
| 70 | + |
| 71 | +let explicitExample = Number("10"); |
| 72 | +console.log(explicitExample); |
| 73 | + |
| 74 | +let edgeCaseNaN = Number("Hello"); |
| 75 | +console.log(edgeCaseNaN); |
0 commit comments