how do I finish this program? (javascript) [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The code below belongs to a crossrider extension I am currently attempting to develop that hopefully will sync bookmarks across browsers. This file is currently the background.js file it will first retrieve a snapshot of the bookmarks file from the local database then compare that to the current list of bookmarks and if there are any differences (either additions to the bookmarks list or subtractions) they will be be returned with the getChanges() function and then sent to the server, finally the script updates the snapshot and waits 30 seconds before restarting the process. I dont really know how to make the getChanges() function. It needs to return presumably a json object indicating both the additions and subtractions (both their titles and urls). If someone could write the code for the function that would be great. Thanks
appAPI.ready(function() {
// Poll every 30 seconds
setInterval(function() {
appAPI.db.async.get('prevBookmarks', function(value) {
// Load or initialize previous bookmarks list
var prevBookmarks = (value) ? value : {};
// Get current bookmarks
appAPI.bookmarks.getTree(function(nodes) {
// Save bookmark list for next comparison
appAPI.db.async.set('prevBookmarks', nodes);
// In your getChanges functions, traverse the bookmark trees collating
// changes and then post then to your API server using appAPI.request
var changes = getChanges(prevBookmarks, nodes);
appAPI.request.post({
url: http://yourAPIserver.com,
postData: changes,
contentType: 'application/json'
});
});
});
}, 30 * 1000);
});

Ok, you've got jQuery as one of your tags, so try this link: Compare 2 arrays which returns difference.
It returns the differences between two arrays. You'll have to perform this twice for what you're doing, once to figure out what is in current that is not in previous and vice versa. I don't know what properties are contained in your bookmarks so this simple example might not exactly suit your needs, but it might point you in the right direction.
Good luck and welcome to JavaScript!

Related

How would I setup a HTML page to have a new title each new day? [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
So my ultimate goal here is to have the site have a new title each day (24 hours).
I am not a very experienced program, but, I am aware something similar could be done with JS.
I saw this idea:
setInterval(function() {
//change title
//document.title = "Some new title";
}, 3000);
I'm not sure how I can take this idea above, which I do not fully understand and make it use, for example, a large array or predefined titles and select one at random each day.
Would it be possible to select the title out of another file or should I have them all in the JS file? On the question just asked, should I have the JS code in the HTML file itself or referenced as a file like a CSS file?
I really appreciate any walkthrough/help I can get on this. I hope your days are well all.
found the solved problem with running code once per day:
Run code once a day
When you will have a function that runs once per day, you just have to reference the title from DOM inside the function and define new title.
var titles = ["title1", "title2", "title3"];
var iterator = 0; // this variable should be incremented every day
// somewhere inside the function that runs once per day
document.title = titles[iterator];
There are 2 ways to import a script on html page.
Inline: https://www.w3schools.com/tags/tag_script.asp
External: https://www.w3schools.com/tags/tag_link.asp
U can even read external file (.txt e.g.) in js, you can look that up, but it's bit more complicated than this.

Firebase optimize multiple request with same data [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a simple react app with firebase, i have my user logged in
I have a button, when is pressed i need to get the current user score in firebase ( first request) and increment this value with a random number (second request)
Get user
firebase.database().ref('user' + uid).child().once()
Update user :
firebase.database().ref('user' + uid).update({score : newValue})
Each time user press the button i made this two firebase request to get the current score and increment it
In most case user can click 150 to 600 times on the button whose can cause slowness
Do you have any suggestion to optimize my app and avoid of calling 1200 ( 2 request by 600 click) times firebase ?
why can't you try "transaction" query on the firebase.The update function takes the current state of the data as an argument and returns the new desired state you would like to write. what you have to do is just pass the key to the transaction like below
firebase.database().ref('user' + uid).child('score')
.transaction(function(current){ // current will hold current value of the key
return current + newValue ; // this return will automatically updates the new value to the firebase
});
Transaction is really good for these scenarios Follow up this link for more deets
So if you use transaction then you can reduce two queries into one.
I Hope this helps.

How to insert/update data in firebase using 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 5 years ago.
Improve this question
I have a firebase database where a node structure is like this.How do I insert/update data from a web page using javascript? I want to insert data individually in promo_ar,promo_en and promo_fr.
I am new in firebase and the help will be much appreciated!
You must do it this way
firebase.database().ref('Promotions/promo_en').set({
desc_promo: value,
name_promo: value
});
Remember that using set, you update elements of a node of your database, on the other hand, when you perform this type of updates, you must send both elements to update within that node, if you omit any element, it is removed from your base, for example...
firebase.database().ref('Promotions/promo_en').set({
desc_promo: value,
});
In this case, only desc_promo is being updated, thus eliminating the rest of the node elements.
in the same way you can insert new nodes in an element like this
firebase.database().ref('Promotions/promo_es').set({
desc_promo: value,
name_promo: value
});
in this way we add the Spanish language to the Promotions node
AGGREGATE:
you can have as many nodes as you want below a main node, remember that if you are going to update the final node, it does not affect the higher ones, but the higher node does affect the lower ones, for example
firebase.database().ref('Promotions/promo_es/exchange').set(
{
desc_promo: value,
name_promo: value
}
);
Promotions
|_>promo_es
|_>desc_promo : value
|_>name_promo : value
|_>exchange
|_>desc_promo : value //<-this is updated or created
|_>name_promo : value //<- this is updated or created
now if you edit the Promotions / promo_es node without including its sub nodes, these will be eliminated, you can perform the tests that you deem convenient in firebase to polish the method to use in your system

jQuery parseInt() not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have this code to add elements to my table and that part works perfectly fine. However, I want to add up the values of the last column in order to later print that on another part of the page. parseInt doesn't work for some reason (the values don't come back as integers and stay as strings, is there anything I have done wrong? Again, adding to the table works perfectly fine.
var hourC = 0;
var hourC =+ parseInt($(".hourCount").text(), 10);
alert(hourC);
Edit:
When I print the values of the variable hourC they don't add up to the previous value, they just stay next to each other. Example: 1 + 1 = 11 rather than 2. I don't see where my issue is and the answer for debugging didn't help since I still got the same result.
Final Edit:
I achieved what I wanted now through a different medium, I created an array and pushed the values into the array and then I used "join" to solve the issue.
If interested in what I was asking for here is a fiddle with the final result. (You can just change the console.log to alert)
Basic debugging skills.
parseInt works -- its a well tested function. If it broke in a browser, a lot of people would notice.
However, you haven't given any way to figure out what is going on with your code, since the real problem MUST be here:
$(".hourCount").text()
That must not contain whatever value you think it does.
Split your code up and use the debugger, or even console log, to see what the values are.
var hourC = 0;
var strValue = $(".hourCount").text();
alert(["strValue = ", strValue]);
hourC = parseInt(strValue, 10);
alert(hourC);

Local Variable usage reasoning [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have lot enough functions, which look like:
var functionName = function(e) {
//
};
where all the parameters are getting passed in in a single container e. Most times values are simple values (no functions), ex.:
{ parameter1: 1, parameter2: "Name", parameter3:{ subParameter1: "A"}}
But there're times when I pass in functions as in: { p2:function(){...} }
I have two options when it comes to utilising parameter values:
Options 1: get parameter values from the chain, starting from e: e.parameter1, e.parameter3.subParameter1 etc.
Option 2: use cached parameter values:
var parameter1 = e.parameter1;
var subParameter1 = e.parameter3.subParameter1;
The second option improves readability but increases the number of vars and the size of the code base. On another hand it's much drier when using long chains, i.e. e.p1.p2.p3 etc.
What reasoning should I use for choosing between those two options?
**Update 1 - the question sounds quite subjective, let me re-prase it.**
I don't mind using chains all the way, no local vars codebase is smaller, I can always figure out what's what, are the any cases when caching is a must?
A combination, based on depth(e.p1 vs e.p1.sp2.ssp3) and frequency of use. Deeper sub-properties and high usage of any sub-property both benefit from caching.
Nested property look ups can get costly, and caching the value after executing the look up once is valuable if you're going to use it a lot. This is only more efficient if you're accessing a particular property on the chain more than once, and the more you access it, the more you benefit from caching.
If you only have one level deep(e.p1, e.p2, e.p3) and you're only looking up each property value once, don't bother.
If you're accessing e.p1.sp2.ssp3 all throughout your function, cache it for sure.

Categories

Resources