saving and loading variables [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I made a game and its a type of game which takes hours to complete. Its a browser game. How can I store variables ( need to store 20-40 variables ) and load them using cookies ? ( so that I can access them even if browser is closed and opened again ). Please, I need help.

You would be better off using localStorage and not cookies. Pretty simple if you just do something like
//The defaults the user starts with
var _defaults = {
level : 1,
userName : null,
life : 100
};
//getting previous values from storage
var savedDetails = localStorage.settings ? JSON.parse(localStorage.settings) : {};
var settings = $.extend({},_defaults, savedDetails);
if(!settings.userName) {
//set username and save
settings.userName = window.prompt("name");
} else {
//username is there so say hi
console.log("Welcome back " + settings.userName);
}
//Save this back to the local storage since we made changes
localStorage.settings = JSON.stringify(settings);

Related

Converting a javascript string into an existing javascript variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am stuck with this problem. I have a function to accept the path and also the same time I have a variable that I want to condition with.
Here is my problem: I want to make a string type that will act as an access to my variable.
In my situation, I have a roles.operation variable which I want to access it dynamically.
The roles variable has an array with the values of:
roles.operations = ['document','article','document-type'];
with this variable I want this to be access dynamically.
Here is what I've tried, which in replacePath i have the value of document-type:
export const createVariable = (roles,path) => {
const replacePath = path.replace(/-/g,"_");
const finalPath = window[`roles.operations.${replacePath}`];
console.log(finalPath);
}
this gives me undefined.
Try this way:
const finalPath = window['roles']['operations'][replacePath];
or
const finalPath = window.roles.operations[replacePath];

How to get a child key value in Firebase Realtime Database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm new with Firebase and I'm currently using JavaScript SDK to integrate my web application with Firebase.
My question is: How to get value within the red box that I drew on the image?
I am using
snapshot.key
to access the key valuebut it returns the generated key (blue box) to me which is not the one I wanted.
Based on your comments, it is not 100% clear what you are looking for. If you know that you have a sub-node with the key speechtext under the recording parent node with key LRC2o.... you don't need to query for getting the key of this sub-node (since you know it).
If, on the other hand, you want to iterate on all the keys of the sub-nodes of the recording node, do as follows (based on your code):
var dbRecording = firebase.database().ref("recordings/");
dbRecording.once("value", function(snapshot2) {
if (snapshot2.exists()) {
snapshot2.forEach(function(value) {
var childObject = value.val();
Object.keys(childObject).forEach(e => console.log(`key = ${e}`));
});
}
});
If you want the keys and the values, do as follows:
var dbRecording = firebase.database().ref("recordings/");
dbRecording.once("value", function(snapshot2) {
if (snapshot2.exists()) {
snapshot2.forEach(function(value) {
var childObject = value.val();
Object.keys(childObject).forEach(e => console.log(`key = ${e} value = ${childObject[e]}`));
});
}
});

How to ask in Javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to write a line of code which asks a question and the user has to enter a number. The number is then stored as a variable and used throughout the rest of the script. Such as:
fit1 = "What is your fitness level?"
level = the number the user had entered
something like that. Can't really explain it properly
P.S. I'm writing my code out in gedit because thats what my uni uses.
You can use a prompt like shown below:
var n = prompt("Fitness level");
console.log(n);
window.level = window.prompt('What is my fitness level?');
console.log(window.level); //save in glonal space of window
You can use the Prompt command, and cast the string answer to type number :
var number = parseInt(prompt('Give me a number', '0'));
alert('You said: ' + number);

How to get hold of a part of dynamically generated URL [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm using filestack, when ever I upload an image it creates an unique url for the uploaded Image. I want to get hold of a specific part of a url to a variable.
https://www.filestackapi.com/api/file/qiXTZ0dQUGPQRG4GH0Cy
https://www.filestackapi.com/api/file/"qiXTZ0dQUGPQRG4GH0Cy"
The above Url belongs to an image, but I want get hold of the quoted part to a variable, how to do that?
You can search for the last index of / in the url then make a substring of the url starting from that point.
var url = 'https://www.filestackapi.com/api/file/qiXTZ0dQUGPQRG4GH0Cy';
var lastIndex = url.lastIndexOf("/");
var searched = url.substring(lastIndex + 1);
// searched = qiXTZ0dQUGPQRG4GH0Cy

How to save a variable javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So im running this script and i want to be able to save the variable stopNumber after the script is finished running so that the next time i run this script the saved variable stopNumber can be set to the variable loops so that i dont have to manually set it all the time.
var loops = Number(prompt("Starting csv line?"));
var stopNumber = loops + 15;
for (csvLine = loops ; csvLine <= stopNumber ; csvLine++) {
iimSet ("-var_CSVLINE", csvLine);
iimPlay("Testing.iim");
}
You can store the value in local storage
localStorage.setItem("stopNumber", stopNumber);
Retrieve it with
stopNumber = parseInt(localStorage.getItem("stopNumber"));
More info here
http://www.w3schools.com/html/html5_webstorage.asp
try to use localStorage.
Like:
localStorage.stopNumber = stopNumber;
you may call it as localStorage.stopNumber

Categories

Resources