How to control fromat for big numbers in QML like (-715827883) - javascript

I have a variable like val=1
I have applied certain condition.
When the condition is true then val is multiplied by 3.
like 3, 9, 27, 81, 243..........
but after a long loop it shows like -715827883 value.
Why?
I want my result like,when i start to execute my loop reversely then val/3 is executed and at the end it will give 1.
But now it's giving 0.
Please suggest.

Related

what does '&' do in this solution? [duplicate]

This question already has answers here:
How does '&' work in relation to odd and even? In JS
(3 answers)
Closed last month.
I'm working through a problem on CodeSignal and trying to understand some of the solutions that other people have submitted. One of the solutions was as follows, and I don't understand what the ampersand is doing.
(a) => a.reduce((p,v,i) => (p[i&1]+=v,p), [0,0])
The problem is:
Several people are standing in a row and need to be divided into two teams. The first person goes into team 1, the second goes into team 2, the third goes into team 1 again, the fourth into team 2, and so on.
You are given an array of positive integers - the weights of the people. Return an array of two integers, where the first element is the total weight of team 1, and the second element is the total weight of team 2 after the division is complete.
Example
For a = [50, 60, 60, 45, 70], the output should be
solution(a) = [180, 105].
In this solution, the & operator is used to perform a bitwise AND operation. In JavaScript, the & operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
In the given solution, the & operator is used to determine whether the index i of the current element in the array is even or odd. If i is even, the result of i & 1 will be 0. If i is odd, the result of i & 1 will be 1.

Javascript P5 seems to be storing an array value I change later, even though I log it before it's changed, logs updates version

I'm using P5.js to make a connect 4 game that I later want to use to make some training data for a ML project. I'm just currently working on making some logic. In a separate file, (separate just so I can test ideas) I have it so you hit a number 1-7 as a row number, and then it will color in your spot on the board. I'm using the logic system to know how far down the colored block needs to go. I have some arrays corresponding to the columns, and for testing purposes, I have 4 columns of 3 down. A 1 represents somewhere a piece is, and a 0 is an open space. When in a column, I use a for loop to iterate through, and if i is a 1, and i-1 is a 0, change i-1 to be a 1. This effectively simulates the gravity of dropping a piece down. The problem is, when I run console.log, both before and after my logic, it gives my the same result, but it's the post logic result. I don't know why it won't log the correct pre-logic array. My code is:
row = [2];
nums = [
[0,0,0],
[0,0,1],
[0,1,1],
[1,1,1]
]
console.log(nums[row])
//console.log(nums[row].length - 1)
for (i = 0; i<= ((nums[row].length) - 1); i++) {
//console.log(nums[row])
// console.log(i)
if ((nums[row][i]) == 1 && nums[row][i-1] == 0) {
nums[row][i-1] = 1
}
/*console.log(nums[row][i])*/
}
console.log(nums[row])
Before I run the logic, it shoud log [0,1,1] and after it should be [1,1,1]. Instead, any time I run it on a row that gets changed, it logs the output twice. I don't know why it isn't logging the array before it gets changed first. Any help would be great!
Yes, this can be annoying, and you should read certainly read the linked comment. But for a quick/dirty solution, you can use a function like the following:
function console_log(o)
console.log(JSON.parse(JSON.stringify(o)))
}
then call console_log(nums[row]) instead of console.log(nums[row])

Logic to find the nearest possible sum of array values to target

This is a simpler version of knapsack, which I am having trouble wrapping my head around.
In my version I don't care how valuable the items are. I just want to get as close to the weight capacity as possible, and order doesn't matter because I'm doing it multiple times and shuffling in between.
So to be clear:
I have an array of values like: weights = [{44, 52, 100, 33, 33, 22, 25, 4, 6, 77, 88, 45}] and a capacity of, for example:capacity: 204
I want the closest combination of array values to that capacity number without repeating any, I'm not super great at math, and the wikipedia article has completely lost me.
Can someone explain how to get this?
Naive approach: cycle through all subsets of N numbers, and check the sum of weights. Running time is O(2^N*N)
You can try dynamic programming.
The problem can be divided into 2 subproblems, to check whether the sum of set is equal to or less than the capacity.
1) Include the current element in subset, and recur for the remaining items with remaining sum.
2) Exclude the current element from the subset, recur remaining items.
The base case of the recursion would be when no items are left. Finally, we output the items included in the subset.
Running time is O(n*capacity) in O(n)

Check incrementation of adjacent spots in matrix, JavaScript

I've been using CodeFights for a couple days (which I highly recommend as it's a great way to build on to your skills), and today I came across a challenge that I couldn't figure out for the life of me. I hate to give away solutions, but nothing I tried would work.
Generally, the challenge was this: Given any matrix of numbers, such as
[1,4,5,8]
[2,3,6,7]
as a parameter to a function, return true if, starting in the top-left corner, you could traverse all the numbers in the array, incrementing by one each time, by moving only to adjacent spots. So, I start at 1, 2 is adjacent so I can move there, 3 is adjacent to 2 so I can move there, and so on and so forth. If you can't get through the entire matrix then return false. It seems pretty simple until you get down to it.
you must:
– Keep track and compare the number of items checked, against the number of items in the matrix.
– Somehow check each adjacent spot for the value of the current spot +1, and then make that the current spot. Then keep checking until you run out of numbers, otherwise return false.
I tried so many different ways of doing this, using 3 or 4 levels of nested for loops, and nothing I tried would work for all of the test cases. I probably wont ever see the problem again, but just for knowledge's sake, could anyone point me in the right direction? Namely just helping me to understand how to efficiently compare against adjacent spaces in a matrix (to maybe avoid giving away the answer for future CodeFighters)?
Just to clarify:
The function would be set up passing in a matrix:
function traverse(matrix) {
}
and something like this would return true:
[[ 1, 4, 5, 8]
[ 2, 3, 6, 7]]
and something like this would return false:
[[ 1, 3, 5, 8]
[ 2, 4, 9, 7]
[10,12, 6,11]]
Any help just to understand where I may have gone wrong in my execution is greatly appreciated. (Btw I can't post exactly what I tried because I don't believe there's a way to get back to it on the website.)

The way to increase variable

foo.setAttribute("item-position", ""+bar+"");
The bar variable is a number, for example 1 or 15. What's the way to increase it on 1, so it would be 2 and 16 ?
May be something like this? But it doesn't work.
foo.setAttribute("item-position", ""+bar+""+1);
// The result should be 2 and 16, but here the result is 21 and 161.
// That is not what I want.
You're currently appending the number to the end of the string, this has nothing to do with arithmetic.
Just add the calculated result
foo.setAttribute("item-position", bar+1);
You don't have to turn it into a string, setAttribute will do that part.
Or if you want to increase the value in bar and show it, use the preincrement operator:
foo.setAttribute("item-position", ++bar);

Categories