diff --git a/Assigment-5/problem-1.js b/Assigment-5/problem-1.js new file mode 100644 index 0000000..1bf151a --- /dev/null +++ b/Assigment-5/problem-1.js @@ -0,0 +1,25 @@ +// Problem-Statement-1 + +// What is the difference between i++ and ++i + +// i++ is post incremental value +//i++ is post increment because it increments i's value by 1 after the operation is over. +// Here the value of i will be assigned to j first, and then i will be incremented. + +let i=1; +let j; +j=i++ +console.log(i,j) +// here j value is constant + + +// ++i is pre incremental value +//++i is pre increment because it increments i's value by 1 before the operation. It means j = i; will execute after i++. + +let u=1; +let v; +v=++u +console.log(u,v) // 2,2 + + + diff --git a/Assigment-5/problem2.js b/Assigment-5/problem2.js new file mode 100644 index 0000000..95e9c14 --- /dev/null +++ b/Assigment-5/problem2.js @@ -0,0 +1,7 @@ +// what would happen if you paas index beyond a string or a negative index + +let string="naziya" +console.log(string[9]) // undefined +console.log(string[-2]) // undefined + +//accessing an index beyond the range or using a negative index will not throw an error. Instead, the result will be undefined diff --git a/Assigment-5/problem3.js b/Assigment-5/problem3.js new file mode 100644 index 0000000..7666e7d --- /dev/null +++ b/Assigment-5/problem3.js @@ -0,0 +1,26 @@ +//Do you think JSON.stringify would work for arrays as well? What about nested objects? What happens if we pass numbers, strings, undefined, null to JSON.stringify? +// JSON.stringify for an object + + +// JSON.stringify works for an array,Result will be array in string format +let array=["naaz","neha",9,"shivi"] +console.log(typeof(JSON.stringify(array))) + +// Nested Objects + +let company={ + name:"josh", + employee:{ + name:"naziya", + designation:"QA", + age:27 } + +} +console.log(JSON.stringify(company)) + +// pass numbers, strings, undefined, null to JSON.stringify +let ex; +console.log((JSON.stringify(6)),typeof(JSON.stringify(6))) // 6// typeof-string +console.log(JSON.stringify("naziya"),typeof(JSON.stringify("naziya"))) // "naziya" // string +console.log(JSON.stringify(null),typeof(JSON.stringify(null))) // null ///string +console.log(JSON.stringify(ex),typeof(JSON.stringify(ex))) // udefined // undefined \ No newline at end of file diff --git a/Assigment-5/problem4.js b/Assigment-5/problem4.js new file mode 100644 index 0000000..3a56b5f --- /dev/null +++ b/Assigment-5/problem4.js @@ -0,0 +1,36 @@ +//What happens if you pass a regular/invalid JSON string to JSON.parse? What will happen if such an invalid function runs in the program? Will other parts of the code execute correctly after that? + +// valid json string + +let validJSONString='{"name":"naziya","hobbies":["gym","yoga","bikes"]}' +let validJSONObject= JSON.parse(validJSONString) +validJSONObject.name="naaz"; +validJSONObject.city="satara" +console.log(validJSONObject) + + +//invalid JSON OBJECT + +let invalidJSONString='{"name":"neha","age":23,"designation":"dev",}' // Syntax Error +// let invalidJSONObject= JSON.parse(invalidJSONString) +// invalidJSONObject.name="priya"; +// invalidJSONObject.city="pune" +// console.log(invalidJSONObject) + + +// Error Handling in Javascript + +try{ + let invalidJSONObject= JSON.parse(invalidJSONString) + console.log(invalidJSONObject) +} +catch(error){ + console.error("Error:", error.message); +} +console.log("naaz") + +/*the JSON.parse call with an invalid JSON string triggers the catch block, and the error message is logged. The code after the try-catch block will continue to execute. + +However, if the error is not caught and the exception propagates to the global scope (e.g., it's not within a function with its own try-catch block), it might lead to the termination of the script. Subsequent code in the same script won't be executed. + +It's good practice to handle errors gracefully to ensure that the failure of one part of the code doesn't have a cascading effect on the entire application. Always consider using try-catch blocks when working with potentially problematic operations, such as parsing JSON from external sources.*/