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 have tried multiple codes but none of them have worked. I am trying to get the player's score or "dogs"(with the variable e) to save across sessions. Here's my code https://jsfiddle.net/Zontoon/yxkzd4cb/13/.
var e = 0; //is updated as the user clicks and buys, and I need to save the user's score.
https://jsfiddle.net/ChrisWong/qwf4ey7m/6/
// init or get score from localStorage
var e = localStorage.getItem('dogs') || 0;
e = parseInt(e);
document.getElementById("var").innerHTML = "Dogs: " + e;
...
function myfunction() {
e = e + buttonvalue;
// update the element and the score
updac();
...
}
...
// update the element and the score
function updac() {
document.getElementById("var").innerHTML = "Dogs: " + e;
// save to localStorage
localStorage.setItem('dogs', e);
}
Window.localStorage has the details and I suggest you to read it to understand how to use it.
Related
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 a beginner in JavaScript and I write the following code, I also write console.log(), and I do not get results, can someone help me and explain to me why I can't get results?
function integers() {
let MyArray;
MyArray = [];
for (let i = 2; i <= 10; i++) {
MyArray.push(i);
}
console.log(MyArray);
}
The javascript functions are not being called automatically. If you need to do so, You can create an auto call function - https://stackoverflow.com/a/10704006/7078456
Or you can manually call your function to get the output of your function.
function integers() {
let myArray = [];
for (let i = 2; i <= 10; i++) {
myArray.push(i);
}
console.log(myArray);
}
integers()
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 2 years ago.
Improve this question
On click i created localstorage item "storageKey__list" and value is "example.com"
I need to get all item keys and values ends with "__list" and then get entire key and value.
Result must be:
"storageKey__list , example.com"
You could grab the entries in localStorage using Object.entries() and then use .filter() to obtain only the entires ending in "__list" by using .endsWith():
Object.entries(localStorage).filter(([key]) => key.endsWith('__list'));
Output:
[["storageKey__list", "example.com"]]
A more browser friendly version of the code above could be to use the following:
Object.keys(localStorage).filter(function(key) {
return /__list$/.test(key);
}).map(function(key) {
return [key, localStorage.getItem(key)];
});
Output:
[["storageKey__list", "example.com"]]
Try this:
/** #type {[string, string][]} */
const keyValuePairs = [];
for (let i = 0, l = localStorage.length; i < l; i++) {
const key = localStorage.key(i);
if (key.endsWith("__list"))
keyValuePairs.push([ key, localStorage.getItem(key) ]);
}
console.log(keyValuePairs);
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 5 years ago.
Improve this question
If I select one value the I am getting value is like
val : "ABC"
But if I select multiple value my val is comming in array
val : ["ABC","DEF"].
Now because of this problem is neighther I can work on for single value nor multiple. Can anybody tell me how to handle this.
eg:
var myObj = {
data : "M1",
val : "Abc"
}
After adding another value it become
var myObj = {
data : "M2",
val : Abc,Def
}
function handleVal(val){
if ("string" === typeof val){
console.log("Do sth with string");
} else if (Array.isArray(val)){
console.log("Do sth with array")
}
}
handleVal("ABC");
handleVal([1,2])
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 want to pass var number to callback, after loop.
For example Invenotry array has 25 items.
So after this 25 steps I want to pass number into callback (inCb), because I need to check number value.
var number = 0;
async.eachSeries(inventory, function(item, inCb) {
if(sth == othersthm)
{
number++;
}
}, function(numb) {
check number value and do sth
});
You can just use number:
var number = 0;
async.eachSeries(inventory, function(item, inCb) {
if (sth == othersthm) {
number++;
}
}, function() {
//check number value and do sth
console.log(number);
});
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 8 years ago.
Improve this question
I need to create an array or json that can be filled when detects the counter like an auxiliarJson with variable size but i dont know how can i do it
TypeError: lAttrsPorDia is undefined
lAttrsPorDia[j] = __oATTRS[i];
var lAttrsPorDia;
var j = 0;
for (var i = 0; i < __oATTRS.length; i++) {
if (__oATTRS[i].Dia == counter) {
lAttrsPorDia[j] = __oATTRS[i];
j++;
alert(JSON.stringify(lAttrsPorDia));
}
}
JavaScript arrays already do have variable size:
var arr = [];
arr.push('Hello');