I have an ID generator. I would like the same ID to be assigned to 3 different keys in local-storage. Here is my code:
var ID = function () {
return "_" + Math.random().toString(36).substr(2, 9);
};
let user_name = document.getElementById("username");
let user_paswrd = document.getElementById("password");
let user_email = document.getElementById("email");
let store_data = () => {
let i = 0;
let input_username = localStorage.setItem(
"username_nr" + ID(),
user_name.value
);
let input_password = localStorage.setItem(
"password" + ID(),
user_paswrd.value
);
let input_email = localStorage.setItem("email" + ID(), user_email.value);
};
Now the generator assigns 3 different IDs to the keys "username", "password" and "email". How can I change this so that the generator assigns the same ID to the keys?
>Solution :
Rather than calling ID 3 times, each time generating a new ID, call it a single time and use the same value in each place that it’s needed:
let store_data = () => {
let id = ID();
let i = 0;
let input_username = localStorage.setItem(
"username_nr" + id,
user_name.value
);
let input_password = localStorage.setItem(
"password" + id,
user_paswrd.value
);
let input_email = localStorage.setItem("email" + id, user_email.value);
};