Trying to increment an integer in an Array - javascript

Good Evening,
I am trying to increment an integer that I have index position of '0' in my array, each time my function gets called. I have the variable added with .push, but then I just want to add one to that. I am trying to use indexof(), I have also tried findIndex(). Below is my code
const addFunction = async () => {
var storage_array = await AsyncStorage.getItem(ASYNC_STORAGE_KEY);
try {
if(storage_array) {
storage_array = JSON.parse(storage_array);
let flow_complete = 0;
var foundIndex = storage_array.indexOf(flow_complete);
console.log(foundIndex);
storage_array[foundIndex] = flow_complete++;
await AsyncStorage.setItem(ASYNC_STORAGE_KEY, JSON.stringify(storage_array));
console.log('THIS IS THE ASYNCSTORAGE', storage_array);
} else {
flow_complete = 0;
console.log('Storage array is empty')
}
} catch (error) {
console.log(error);
}
}

Following the re-wording of the issue with your comment:
[...] the objective is to take the number ‘0’ that’s in the 0th position of the array and increment it by 1 each time the function runs
The first issue I see is that you may be misusing the indexOf function. That will not give you the index of an array, but instead the position of a particular value of an array.
Example:
const arr = [9, 2, 7, 14]
const index = arr.indexOf(9) // This will be 0, because the index of the number 9 in this array is 0
const otherIndex = arr.indexOf(7) // This will be 2, because the index of the number 7 in this array is 2
So, for you to access the element in the 0th position, you will want to do arr[0]. So in your code you will want to do the following:
storage_array = JSON.parse(storage_array);
let flow_complete = 0;
// notice there is no need to get the `indexOf` 0 since you do want the position 0
storage_array[0] = flow_complete++;
Now... This will have a second problem which is your usage of the incrementation operator ++. Although this will increment the flow_complete variable, it does not return it to set storage_array[0] as you intend to do so.
To fix this, all you have to do is increment flow_complete prior to assigning it to storage_array[0]. It will look something like this:
let flow_complete = 0;
flow_complete++;
storage_array[0] = flow_complete
However, if my interpretation of your comment above is correct, there is one more problem, which you are assigning flow_complete to storage_array[0] each time the function runs. flow_complete is set to 0 as you can see in your own block of code within the scope of your addFunction, so this means it will always go back to 0 every time it is run.
Going back to your original comment, you want to increment the value in the 0th index of storage_array, not flow_complete itself, correct?
If this is the case, you can completely get rid of the flow_complete variable and instead increment storage_array[0]. This will make your if-block look as follows:
if(storage_array) {
storage_array = JSON.parse(storage_array);
storage_array[0]++;
await AsyncStorage.setItem(ASYNC_STORAGE_KEY, JSON.stringify(storage_array));
console.log('THIS IS THE ASYNCSTORAGE', storage_array);
}

Related

My parameter becomes undefined, why is that?

I am trying to fill my grid with random positions by adding the grid positions to an array and then removing them with a random number as an index. The issue is that it randomly becomes undefined in the middle of my loop. It is the parameter of create_box that becomes undefined.
I'm trying to understand why it becomes undefined, the object gets sent to the function looking as it should. Somehow it doesnt work.
function go(n_columns,n_rows){
let array_of_cells=[];
let number_columns=n_columns+1;
let number_rows=n_rows+1;
for(let columns=1;columns<number_columns;columns++){
for(let rows=1;rows<number_rows;rows++){
let obj={column:columns, row:rows};
array_of_cells.push(obj);
}
}
while(1<array_of_cells.length){
let random_number=Math.floor(Math.random() * array_of_cells.length-1)
array_of_cells.splice(random_number,1);
create_box(array_of_cells[random_number]);
console.log(array_of_cells);
}
function create_box(position){
console.log(position.row)
let box=document.createElement("div");
document.querySelector("#wrapper").appendChild(box);
box.style.backgroundColor="red";
box.style.gridArea = `${position.row} / ${position.column}`;
}
}
go(10, 10);
I think that your issue comes from the fact that you are deleting a value (which reduces the number of indexes in your positions array), before accessing the position at "random_number" index that can sometimes be undefined.
You should probably use the spliced position instead.
while(array_of_cells.length) {
const random_number = Math.floor(Math.random() * (array_of_cells.length - 1)) // note the use of parenthesis here
const position = array_of_cells.splice(random_number, 1)[0];
create_box(position);
console.log(array_of_cells);
}

Apps Script For Loop stuck at 0 in iteration although it is running

I have an array 'vnData' containing 5, 6 rows from where I am trying to extract 3rd column values (based on a criteria) and insert to a new array. Here is my code
for (odr = 0; odr < vnData.length; odr++){
Logger.log(vnData);
tempOdr = vnData[odr][3];
Logger.log(odr);
Logger.log(tempOdr);
Logger.log(vnData[odr][3]);
for(k = 0; k < vnData.length; k++){
if(vnData[k][3] = tempOdr){
odrVal = odrVal + vnData[k][11];
}
}
if(odrVal > 0){
affOdrSet.push(tempOdr);
}
Logger.log(affOdrSet);
}
Logger gives right value of odr in Logger.log(odr); but in Logger.log(vnData[odr][3]); I am always getting a result where value of odr is 0.
So for each iteration I get value from first row. Please help what is wrong in it.
One more thing, if I log Logger.log(vnData[3][3]) in place of Logger.log(vnData[odr][3]) then for first iteration it gives me right value from row 4 but for all subsequent iterations even Logger.log(vnData[3][3]) gives value from first row which is really weird.
The problem is the expression of the first if statement. Instead of
vnData[k][3] = tempOdr
use
vnData[k][3] === tempOdr
The above because = is the assign operator but it's very likely that instead of assigning tempOdr to vnData[k][3] what you want to compare them.

How to update a variable that is returned from a function?

I've been trying to write a go back function. What this function will do is it'll store last two ID number that has been generated and append when there is a new one, delete the first one.
I have this function which creates the IDs and plays the video with that ID.
function newVideo(){
let rando = Math.floor((Math.random() * 3970) + 1);
document.getElementById("vsrc").src = "https://raw.githubusercontent.com/ondersumer07/vinematik-videos/master/vid/" + rando + ".mp4";
document.getElementById("videoEl").load();
return rando;
}
I am returning the rando to use it outside this function, and I can access it outside the function, the problem is, the variable outside the function is not updating everytime newVideo() run.
The code for that goes like this:
let rando = newVideo();
let vidids = [];
if (vidids.length < 2) {
vidids.push(rando)
console.log("added")
} else {
vidids.shift()
console.log("deleted")
};
Basically what this does is to get the returned rando value and push it to the vidids array, delete the first one if there is more than two but it won't, it does not update the let vidids = []; array for some reason. I need it to update everytime the newVideo() function ran.
Also, I want the if to add if there is less then two items in that array and delete from the start of the array if there is more than two items in it. Not really sure if that'll work too.
I can't seem to figure out how to do this, am I doing this whole thing wrong or is there still hope for this function? How am I supposed to write that function?
Edit: I've changed the vidids.length part yet the problem still occur because let rando = newVideo(); doesn't update.
In order to keep the last two Items of an array, you can use something like this.
let vidids = [];
function addToArray(item, array){
array.unshift(item);
return array.slice(0,1);
}
//test
vidids = addToArray(newVideo(), vidids);
vidids =addToArray(newVideo(), vidids);
vidids =addToArray(newVideo(), vidids);
vidids =addToArray(newVideo(), vidids);
vidids =addToArray(newVideo(), vidids);
console.log('Done');
You've made a small mistake that caused your code to not work properly.
The IF condition is intended to check the length of the 'vidids' array,
What actually happens is that it compares the array referance instead of it's length
To fix the issue, add .length after 'vidids' inside of the IF condition.
....
if (vidids.length < 2) {
.....
I figured it out. Basically what I did is storing every number the algorithm creates and then taking one before the last.
Which goes like this:
The algorithm for creating random numbers:
function randomNum() {
let rando = Math.floor((Math.random() * 3970) + 1);
return rando;
};
Then I put this function in a variable:
let videoid = randomNum()
I had another variable called videoids which is above and outside of the randomNum function and is an array:
let videoids = []
After that I stored every number I created with videoid inside videoids by pushing it(This push needs to be inside your function):
videoids.push(videoid);
Okay so I stored all the numbers this way. Now I should take one before the last so I can go to previous video. So I needed to create a function, I used this function which was created by Tadeck, in this thread.
if (!Array.prototype.last) {
Array.prototype.last = function() {
return this[this.length - 2];
};
};
Now I can put that inside of my prevVideo function which looks like this:
function prevVideo() {
document.getElementById("vsrc").src = srcRaw + videoids.last() + ".mp4";
document.getElementById("videoEl").load();
videoids.push(videoids.last());
}
Note: Don't forget to push videoids.last inside your videoids otherwise you can only go to your previous number for once.

JavaScript for loop in a function

I am currently stuck with a javaScript for loop.
The situation is like this, in my program there is a function which returns true/ false value in every 200 ms.
The function, which I am currently trying to code, should obtain the value from the above function ( for the ease of reference, I would name it as, function1) and store it in an array.
I am trying to use a for loop to store those values in an 8 element array.
Shown below is my code,
function myFunction1(imagestatus) //
{
var statusArray = ["","","","","","","",""];
for (var i = 0; i <= statusArray.length - 1; i++)
{
statusArray[i] = imagestatus;
}
}
Now, during the first execution of the for loop, it will assign the 0th element of the array, true or false. And during the second execution also it will do the same which is no good.
The task I expect to do is, when the function1 returns its value to myFunction it must store it in the 0th element. Then when it returns again, if its as same as the value in 0th element, store it in the 1st element, if not same, then take a different action.
Start with an empty array:
var array[];
then use:
array.push(data);
To add each datum to the right end of the array.
Sounds like a number of things need to happen. First, you are asking for a callback function. Second, you need to move the status array to the global scope.
var statusArray = ["","","","","","","",""];
function myFunction1(imagestatus, callback, differentAction) //
{
var i = 0;
// if its as same as the value in 0th element,
while (statusArray[i]==imagestatus)
{
i++;
}
if (i<statusArray.length && i>0)
{
// store it in the 1st element
statusArray[i]=imagestatus;
if (typeof(callback)=="function")
{
callback();
return;
}
}
// if not same, then take a different action
if (typeof(differentAction)=="function")
{
differentAction();
return;
}
}

I'm having trouble writing a recursive function in JavaScript - it doesn't seem to "fall back" to the lesser depth correctly

var diceToRoll = [2,2];
var diceRolled = new Array();
function recurse(diceToRoll, diceRolled) {
roll = diceToRoll[0]
diceLeftToRoll = diceToRoll;
diceLeftToRoll.shift();
for(loop=1; loop<(roll+1); loop++) {
result = diceRolled;
result.push(loop);
if(diceLeftToRoll.length == 0) {
console.log(result);
result.pop();
} else {
recurse(diceLeftToRoll, result);
}
}
}
recurse(diceToRoll, diceRolled);
I'm trying to write a recursive function which prints the possible results of any number of dice. For example, a dd100 (diceToRoll = [6, 10, 10, 100])(diceToRoll = [6, 6, 6, 6, 6]) etc. In the example I have used the simplest case(or two 2-sided dice).
I expected the results to be [1,1], [1,2], [2,1], [2,2] however it only logs [1,1], [1,2]. This is the same with any number or type of dice - only the deepest level of recursion works correctly.
I figure I'm missing something obvious in the logic of it / or misunderstanding variable scope in JavaScript, but I'm just really struggling to get my head around it.
Edit 1 (To make explanation of program's purpose clearer)
The programs purpose is to list all possible values on any number of dice. So a dice 6 implies the range of values 1..6. Likewise, a two-sided dice, 2, implies the range of values 1..2. So for two two-sided dice in the example (diceToRoll[2,2]) the possible values are 1,1 1,2 2,1 and 2,2 - which is what should be returned.
There are several issues with your code:
Use var keyword in order to define local variables.
Assigning array to another variable not copies its content, just reference to the same array. Use Array.slice() if you want to clone array.
Here is a fixed function:
var diceToRoll = [2,2],
diceRolled = [];
function recurse(diceToRoll, diceRolled) {
var roll = diceToRoll[0],
diceLeftToRoll = diceToRoll.slice(1),
loop,
result;
for(loop=1; loop<=roll; loop++) {
result = diceRolled.slice(0);
result.push(loop);
if(diceLeftToRoll.length === 0) {
console.log(result);
result.pop();
} else {
recurse(diceLeftToRoll, result);
}
}
}
recurse(diceToRoll, diceRolled);
NOTE
diceToRoll = diceToRoll.slice(1)
is equivalent to
diceToRoll = diceToRoll.slice(0);
diceToRoll.shift();
Fiddle here: http://jsbin.com/isebef/1/edit
You must declare "roll" (and other local variables) with var.
function recurse(diceToRoll, diceRolled) {
var roll = diceToRoll[0]
var diceLeftToRoll = diceToRoll;
diceLeftToRoll.shift();
for(var loop=1; loop<(roll+1); loop++) {
var result = diceRolled;
result.push(loop);
if(diceLeftToRoll.length == 0) {
console.log(result);
result.pop();
} else {
recurse(diceLeftToRoll, result);
}
}
}
Without var, "roll" and "loop" are global.
I'm not sure what the point of "result" is; it's just a reference to the "diceRolled" array, so I'm not sure why you wouldn't just use that.
edit — I'm not sure exactly what your code is trying to do here, but another significant problem is this:
var diceLeftToRoll = diceToRoll;
diceLeftToRoll.shift();
When you make an assignment like that of a value that's a reference to an array, you do not make a copy of the array. Thus both variables refer to the same array object, and the first element will be removed from it. If you instead make "diceLeftToRoll" a copy of the other array, then things work differently:
var diceLeftToRoll = diceToRoll.slice(1); // copy all but 1st element
I don't think the whole thing will work, however, because I now think that the "result" variable was an attempt to do something similar.
edit again Here is an alternative version that returns results in a list. This one avoids making copies except for the final entries added to the result.
function allRolls( dice ) {
var list = [], rolled = [];
function roll( dn ) {
var dp = rolled.length;
for (var dv = 1; dv <= dice[dn]; ++dv) {
rolled[dp] = dv;
if (dn < dice.length - 1)
roll(dn + 1)
else
list.push(rolled.slice(0));
}
rolled.length = dp;
}
if (dice.length) roll(0);
return list;
}
allRolls([3, 3, 3]);
The function involves an inner function that does all the work. It's passed the index in the "dice" list of a dice to roll; initially, that's 0.
The function keeps track of two other lists: the accumulated possible rolls, and an array representing the "rolled so far" for use by the recursive inner function.
At each recursive level, the function iterates through the faces of the current dice (that is, dice[dn]). Each iteration places that dice value in a slot at the end of the "rolled" array; the same slot is used for each iteration. Now, if the loop notices that "dn" represents the last dice in the list, then it makes a copy of the "rolled" array and adds it to the result list. If not, then it makes a recursive call, passing the next dice index down.
The outer function then just checks to see if there are any dice to roll, and rolls them if so. It returns the accumulated list.

Categories

Resources