In javascript I get two numbers let's call them x & y and an array of integers with random int's from 0 - 10 arrayints.
x is the number I'm trying to get by combining y with any of the numbers in arrayints.
for example: lets say x = 8 and y = 3 and arrayints consists of numbers arrayints(1,7,2,7,4,5)
so x could equal = y + 5
or
x = y + 1 + 4
All the values in x, y and arrayints will be random and always <= 10.
Please advise if more information is needed and everything will be in javascript or jquery no fuss as far as my code goes I will copy and paste but it will just be one blob of incromprehensible letters which are giving me an headache.
function makex(x,y) {
//this is how I get the array of random ints <=10
$("#div").children().each(function(n, i) {
var id = parseInt(this.id+"");
});
}
Here's a recursive solution that returns an array of the integers that add up to x, including y (or an empty array if it doesn't exist). If you want to exclude y, then feel free to make a wrapper for this.
function make(x, y, intOptions) {
var z = x - y
if (intOptions.indexOf(z) !== -1) {
return [y, z];
} else if (intOptions.length > 1){
var i = intOptions.length;
var ans;
while (i--) {
ans = make(z, intOptions[i], intOptions.slice(0, i))
if (ans.length) {
return [y].concat(ans);
}
}
}
return [];
}
Related
This is part of a bigger problem I try to solve in an exercise. It looks like this:
x is 10 times more likely to appear than y.
z appears 2x less often than y.
I solved this by calculating a single unit like this:
const x = 100;
const y = 10;
const z = 5;
const unit = 100 / (x + y + z);
unit equals 0.87
So when I do (0.87) + (0.87 * 10) + (0.87 * 5) I get 100%(almost)
Then I generate a random number between 0 and 1.
const randomNumber = Math.random();
function getValue() {
if (randomNumber <= 0.87) {
console.log('x');
} else if (randomNumber > 0.87 && randomNumber < 95.7) {
console.log('y');
} else console.log('z');
}
getValue();
If value<0.87 then I log out x, if value < 0.87+(0.087*10) I log y etc
Can anyone recommend a more logical and elegant way than this?
Your way looks clean for me except the fact that randomNumber > 0.87 is redundant.
if you store the value x, y and z in an array, you can probably write some cleaner code for example:
let prob = [100, 10, 5];
let sum = prob.reduce((a, b) => a + b, 0);
let normalizedProb = prob.map(p => p / sum);
let cummulativeProb = normalizedProb.map((cummulative => p => cummulative += p)(0));
for (let i = 0; i <= 50; i++) {
let r = Math.random();
console.log(cummulativeProb.filter(p => r >= p).length);
}
Also, you may want to read this post for faster implementation (in python though). However, the code will be more complicated for sure.
Since the weights are small integers, you can duplicate the x, y and z in an array, and just pick one random cell of the array:
let choices = "zyyxxxxxxxxxxxxxxxxxxxx";
console.log(choices[Math.floor(Math.random() * 23)]);
Here the magic number 23 is the number of choices, 1+2+20; and Math.floor(Math.random() * 23) is a random integer uniformly at random in range [0, 22] (both bounds included). See also:
Generating random whole numbers in JavaScript in a specific range?
Below is my code
function generateGrid(spacing, boundBox, geometry) {
console.log(spacing);
console.log(boundBox);
var grid = [];
console.log(spacing);
for (var x = boundBox[0]; x < boundBox[2]; x = x + spacing) {
for (var y = boundBox[1]; y < boundBox[3]; y = y + spacing) {
if(geometry.intersectsCoordinate([x, y])) grid.push([x, y]);
console.log(boundBox[3] - y)
}
console.log(boundBox[1] - x)
}
console.log(grid);
}
If spacing is replaced by a number like 10000 the for loop executes fine.
From your Console screenshot it looks like the passed in argument is the string "10000" rather than the number 10000.
Either check the code that's calling your function, or convert to an integer inside the function, for example by using parseInt(spacing).
As a tip to help with spotting any similar issues in the future, Chrome's console.log shows numeric values in blue and string values in black.
x is a number so use String(x) so you use operator + between two strings, that would give you "15" + "1" = "151", but that is probably not what you wanted
My function needs to call a number of index values based on the users input (1-5) based on job duties they want to see and then call from the array the index values, so if the user inputs 3, index values 0,1,2 need to be written. Currently it only writes the one associated with the input not all below that input. This is what I have so far:
function jobduties() {
var x = document.getElementById("input").value;
var y = x-1;
var duties = ["Sales", "Customer Service", "Management", "Driving", "Cleaning"];
var z = " ";
while(x > y){
z += z + duties[y];
y++;
document.getElementById("print").innerHTML= z;
}
}
The problem is that you're setting y = x-1, so if the user enters 3 (x=3), y will be 2 (x-1)... In your while you start with y = 2, you add that to z, and then increase y, so you are only evaluating y = 2, since the next one is y=3, and that doesn't satisfy your condition (x > y)
Try these changes...
change var y = x - 1;
for var y = 0;
And also remove the + where you edit the z variable.
Edit: By the way, I just answer following the method you choosed, but I strongly recommend you to use a FOR loop instead, much easier..
I am new to coding and javascript and was asked, for an assignment, to convert base 10 numbers to a binary base without using specific Javascript built in methods (like alert(a.toString(16))), and I am only allowed to use loops,arrays and functions. This is what i have so far:
var number = prompt("Enter an unsigned base 10 number");
if (number>=0) {
var base = prompt("Enter b for binary, o for octal, or h for hexadecimal");
if (base=="h"||base=="H") {
;
}
So as you can see, I don't have much to go on. I was curious as to what equation or formula I would use to convert the base 10 number, as well as how i'm supposed to show A=10, B=11, C=12 and so forth for a hexadecimal base. Any help would be greatly appreciated!
edit: This is a rather complicated way to do it,
as Alnitak showed me (see discussion below).
It is more a scibble, or the long way by foot.
Short explanation:
If we want to get the binary of the decimal number 10,
we have to try 2^n so that 2^n is still smaller than 10.
For example 2^3 = 8 (that is OK). But 2^4 = 16 (thats too big).
So we have 2^3 and store a 1 for that in an array at index 3.
Now we have to get the rest of 10-2^3, which is 2, and have to
make the same calculation again until we get a difference of zero.
At last we have to reverse the array because its the other way arround.
var a = prompt("Enter an unsigned base 10 number");
var arr = [];
var i = 0;
function decToBin(x) {
y = Math.pow(2, i);
if (y < x) {
arr[i] = 0;
i++;
decToBin(x);
} else if (y > x) {
i--;
newX = (x - Math.pow(2, i));
arr[i] = 1;
i = 0;
decToBin(newX)
} else if (y == x) {
arr[i] = 1;
result = arr.reverse().join();
}
return result;
}
var b = decToBin(a); // var b holds the result
document.write(b);
In a sequence of number starting at (n>0) I need to find two numbers that when multiplied by each other return the same value as the sum of the sequence less the sum of those numbers (sum - (x+y)). I already have one program written, but I believe is not efficient enough.Is there a way of getting the same result without nesting loops?
Here is my code:
function removeNb (n) {
result = [];
var sum = (n*(n+1))/2
for(var x=n; x > 0; x--){
for(var y = 1; y<=x; y++){
z= x*y;
var r = sum-(y+x);
if(z == r){
result.push([y,x],[x,y])} *//because with inverted loops (y++, x--) only unique values are left.*
}
return result;
}
Your solution so far has O(n^2). As far as I see the sequence is not a random one. It is the sequence 1, 2, 3 ... N. I assumed that from following line sum = (n*(n+1)) / 2.
Now let's look at the equation r = z which is actually x*y = sum - x - y. You can make a single loop iterating over x from 1 to n and calculate y. Y = (sum - x) / (x +1). If y is equal or less than N so you have a solution(x,y). Using this approach you will make a single loop and you complexity will be O(n).
You can also precompute values and store tham as array as well. This solution is useful if n is in some small range.