|
1 | 1 | console.log("-------------------------------------------------");
|
2 | 2 | console.log("Activity 1: ");
|
3 | 3 |
|
| 4 | +// Task 1: Write a script to save a string value to localStorage and retrieve it. Log the retrieved value. |
| 5 | + |
| 6 | +const { LocalStorage } = require('node-localstorage'); |
| 7 | +const localStorage = new LocalStorage('./scratch'); |
| 8 | +localStorage.setItem('myStringKey', 'Hello, World!'); |
| 9 | +const retrievedString = localStorage.getItem('myStringKey'); |
| 10 | +console.log(retrievedString); // Output: Hello, World! |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +// Task 2: Write a script to save an object to localStorage by converting it to a JSON string. Retrieve and parse the object, then log it. |
| 15 | + |
| 16 | +// Define an object |
| 17 | +const myObject = { |
| 18 | + name: 'Yash', |
| 19 | + age: 24, |
| 20 | + profession: 'Software Engineer' |
| 21 | +}; |
| 22 | +localStorage.setItem('myObjectKey', JSON.stringify(myObject)); |
| 23 | +const retrievedObject = JSON.parse(localStorage.getItem('myObjectKey')); |
| 24 | +console.log(retrievedObject); // Output: { name: 'Yash', age: 24, profession: 'Software Engineer' } |
4 | 25 |
|
5 | 26 |
|
6 | 27 | console.log("-------------------------------------------------");
|
|
0 commit comments