how can I add storage in this code?
When a user refreshes the page, I wish that the countdown does not start from 0. Thanks
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function random_countdown(hour_sel, min_sel, sec_sel) {
random_hour = getRandomInt(3, 3)
random_min = getRandomInt(14, 14)
random_sec = getRandomInt(59, 59)
setInterval(function() {
random_sec = random_sec - 1;
if (random_sec < 0) {
random_sec = 59
random_min = random_min - 1
}
if (random_min < 0) {
random_min = 59
random_hour = random_hour - 1
}
jQuery(hour_sel).text(random_hour)
jQuery(min_sel).text(random_min)
jQuery(sec_sel).text(random_sec)
// console.log (random_hour+' '+random_min+' '+random_sec)
}, 1000)
}
random_countdown('.hours_sel', '.minutes_sel', '.seconds_sel')
You can use localStorage to do that in a browser.
This variable has setItem and getItem methods to save and recover data.
You can learn more here
Related
I cant get my if statements to run it will only run the count down portion but the specifics like when it gets to 5 and 0 it just wont run those extra if statements.
function Countdown()
{
var currTime = 10;
var i = 10;
while (i>=0) {
if (currTime == 5)
{
setTimeout(function () {
document.getElementById("counter").innerHTML = "Warning Less than ½ way to launch, time left = " + currTime;
currTime = currTime - 1;
}, 1000 * i);
i -= 1;
}
if (currTime == 0)
{
document.getElementById("counter").innerHTML = "Blast OFF";
}
else
{
setTimeout(function () {
document.getElementById("counter").innerHTML = "the time left is " + currTime;
currTime = currTime - 1;
}, 1000 * i);
i -= 1; /* same as i = i-1 */
}
};
}
Actually your while loop execute the currTime 11 times here. The main issue is here you are using interval of 1 sec or 1 sec+ etc, but your loop runs very fast in ms of 100 or 500 which excecute currTime very fast. That's the issue here. I hope you got the issue.
I have written a program in javascript (Node) that is supposed to calculate the RSI using the Binance api.
The only thing is that my program does not calculate the "real" RSI. For example, if i set my period to be 14 days, as the formula says - I get an RSI value equal to ~28 and the real RSI being 38.
If i change the period and set it to 20, i get an RSI pretty close to the real one, with mine being 39 and the real one being 38.
I can't figure it out.what am i doing wrong. Any suggestion?
Here is my code :
const binance = require('node-binance-api')().options({
APIKEY: 'xxx',
APISECRET: 'xxx',
useServerTime: true,
test: true // True = SandboxMode
});
/* VARIABLES */
let listClose = [];
let changeUp = 0;
let changeDown = 0;
let last_closeHigh = 0;
let last_closeLow = 0;
let current_time = Date.now();
let period = 20;
function calculateRSI() {
console.log("Generating RSI");
binance.candlesticks("ETHBTC", "1d", (error, ticks, symbol) => {
for (i = 0; i < ticks.length; i++) {
let last_tick = ticks[i];
let [time, open, high, low, close, volume, closeTime, assetVolume, trades, buyBaseVolume, buyAssetVolume, ignored] = last_tick;
listClose.push(close);
if (i == ticks.length -1 ) {
for (x = 0; x < ticks.length; x++) {
previous_close = (parseFloat(listClose[x-1]));
current_close = (parseFloat(listClose[x]));
// HIGH
if (current_close > previous_close) {
upChange = current_close - previous_close;
changeUp += upChange;
if (x == ticks.length -1) {
last_closeHigh = current_close - previous_close;
}
}
// LOW
if (previous_close > current_close) {
downChange = previous_close - current_close;
changeDown += downChange;
if (x == ticks.length - 1) {
last_closeLow = previous_close - current_close;
}
}
if (x == ticks.length-1) {
AVGHigh = changeUp / period;
AVGLow = changeDown / period;
Upavg = (AVGHigh * (period -1) + last_closeHigh) / (period);
Downavg = (AVGLow * (period -1) + last_closeLow) / (period);
RS = Upavg / Downavg;
RSI = (100 - (100 / (1 + RS)));
console.log(RSI);
return RSI;
}
}
}
}
}, {
limit: period,
endTime: current_time
});
}
calculateRSI();
Sorry for jumping on this late...but the reason for this is using the Sandbox.
Binance Sandbox is awful and should never be used, it gives bad values.
I'm trying to solve this leetcode problem here: https://leetcode.com/problems/next-closest-time/description/.
Simply my solution is to try every combination that is valid and then set my ans variable to the one that is closest to the original time. It does print out all the combinations however the diff variable never changes. In the for loop after (if cur.length ==4) it prints every possible combination because it still thinks that diff is 9007199254740991. Also when I assign ans to cur.slice(), I receive a blank array. Even when I uncomment it in the nextClosestTime function I still have the same issue.
/**
* #param {string} time
* #return {string}
*/
var diff = 9007199254740991;
function calcSeconds(digits) {
var hours = (digits[0] * 10) + digits[1];
var seconds = (digits[2] * 10) + digits[3];
return (hours * 3600) + (seconds * 60);
}
function nextClosestTime(time) {
var digits = [];
// diff = 9007199254740991;
var cur = [];
var ans = [];
for (var i = 0; i < time.length; i++) {
if (isFinite(time.charAt(i))) {
digits.push(parseInt(time.charAt(i)));
}
}
var target = calcSeconds(digits);
nch(digits, cur, diff, target, ans);
return ans;
};
function nch(digits, cur, diff, target, ans) {
if (cur.length == 4) {
var curSec = calcSeconds(cur);
if (Math.abs(curSec - target) < diff) {
console.log(cur);
diff = Math.abs(calcSeconds(digits) - calcSeconds(cur));
//ans = cur.slice();
}
return;
}
for (var i = 0; i < digits.length; i++) {
if ((((cur[0] * 10) + cur[1]) >= 24) || (((cur[2] * 10) + cur[3]) >= 60)) {
return;
}
cur.push(digits[i]);
nch(digits, cur, diff, target, ans);
cur.pop()
}
}
I want get array with random unique figures.
I make cycle for
var row = [];
var count = 10;
function getRandomArbitary(min, max) {
return Math.random() * (max - min) + min;
}
function searchRandom() {
var rdm = Math.floor(getRandomArbitary(1, 20));
for (var i = 0; i < row.length; i++) {
if (rdm == row[i]) {
searchRandom();
}
}
row.push(rdm);
}
And then if I want 10 figures in array I make next cycle
for (var i = 0; i < count; i++) {
searchRandom();
}
console.log(row);
But it doesn't work (
Wouldn't it be easier to just make the function take the options as arguments and return the array ?
function randomArray(count, min, max) {
if (count > (max - min)) return;
var arr = [], t;
while (count) {
t = Math.floor(Math.random() * (max - min) + min);
if (arr.indexOf(t) === -1) {
arr.push(t);
count--;
}
}
return arr;
}
console.log(randomArray(10, 1, 20));
I think and i havent ran it on my system
but this should work
function searchRandom() {
var rdm = Math.floor(getRandomArbitary(1, 20));
for (var i = 0; i < row.length; i++) {
if (rdm == row[i]) {
searchRandom();
return;
}
}
row.push(rdm);
}
Notice the return statement after the nested searchRandom()
This will stop you from getting into incorrect cases for example lets say till now the row has [1,2]. and the next random no is 1;
in that case why you are recursively calling the search method??
you can just loop from 1 to len (or 0 to len-1), and wait until you generated the random number that is not present simply by a while loop. here is an example
var generateRandomArray = function(len, min, max) {
if(len> (max-min)) {return;}
var array = [],
getRandomArbitary = function(min, max) {
return Math.floor(Math.random() * (max - min) + min);
},
idx;
for (idx = 0; idx < len; idx++) {
var num;
while (array.includes(num = getRandomArbitary(min,max))) {}
array.push(num)
}
return array;
}
console.log('Random Array of 5 element from 10 to 20: ', generateRandomArray(5, 10,20))
I am trying to make a JS counter to reach a random number and reset it self once it reaches the number and repeat again in 5 seconds.
For example: Random Number is 0.05.
0.00 > 0.01 > 0.02 > 0.03 > 0.04 > 0.05 > 0.00
<div id="current">0</div>
JS
var randomNum = Math.random();
if ( current <= randomNum ) {
for (current = 0; current < randomNum; current+=0.01) {
setInterval(function(){
current += .01;
},1000); } }
else {
current = 0;
}
You could use a closure over the variables and make a check inside of the callback, if greater then the wanted result.
This proposal uses setInterval for counting and setTimeout for the waiting time of 5 sec and the restarting with a new random value.
function startInterval() {
var randomNum = Math.floor(Math.random() * 8) + 2,
current = 0,
interval = setInterval(function() {
current += .01;
if (current > randomNum / 100) {
current = 0;
clearInterval(interval);
setTimeout(startInterval, 5000);
}
document.getElementById('current').innerHTML = current.toFixed(2);
}, 1000);
}
startInterval();
<div id="current">0</div>
Keep a counter variable outside of the loop and then simply clear it, when the desired value is reached.
var randomNum = Math.random() * 25;
var currentValue = 0;
var counter;
counter = setInterval(function() {
if (currentValue < randomNum) {
//Carefull with "0.1" as JavaScript doesn't like it!
currentValue = (currentValue * 10 + 1) / 10
}
if (currentValue > randomNum) {
currentValue = randomNum;
clearInterval(counter);
}
console.log(currentValue, '/', randomNum)
}, 1000 / 60)