I've got a javascript function which generates and returns a new array (of arrays):
function getFees(id){
var prep = new Array, primary = new Array, secondary = new Array, vce = new Array;
prep[0] = 733;
primary[0] = 792;
secondary[0] = 879;
vce[0] = 1108;
if (id == 2) {
prep[1] = (prep[0] - prep[0] * 5 / 100);
prep[1] = Math.ceil(prep[1]);
primary[1] = (primary[0] - primary[0] * 5 / 100);
primary[1] = Math.ceil(primary[1]);
secondary[1] = (secondary[0] - secondary[0] * 5 / 100);
secondary[1] = Math.floor(secondary[1]);
vce[1] = (vce[0] - vce[0] * 5 / 100);
vce[1] = Math.floor(vce[1]);
} else if (id == 3) {
prep[2] = (prep[0] - prep[0] * 10 / 100);
prep[2] = Math.ceil(prep[2]);
primary[2] = (primary[0] - primary[0] * 10 / 100);
primary[2] = Math.ceil(primary[2]);
secondary[2] = (secondary[0] - secondary[0] * 10 / 100);
secondary[2] = Math.floor(secondary[2]);
vce[2] = (vce[0] - vce[0] * 10 / 100);
vce[2] = Math.floor(vce[2]);
} else if (id == 4) {
prep[3] = (prep[0] - prep[0] * 50 / 100);
prep[3] = Math.ceil(prep[3]);
primary[3] = (primary[0] - primary[0] * 50 / 100);
primary[3] = Math.ceil(primary[3]);
secondary[3] = (secondary[0] - secondary[0] * 50 / 100);
secondary[3] = Math.ceil(secondary[3]);
vce[3] = (vce[0] - vce[0] * 50 / 100);
vce[3] = Math.floor(vce[3]);
} else if (id >= 5) {
prep[4] = (prep[0] - prep[0] * 75 / 100);
prep[4] = Math.floor(prep[4]);
primary[4] = (primary[0] - primary[0] * 75 / 100);
primary[4] = Math.ceil(primary[4]);
secondary[4] = (secondary[0] - secondary[0] * 75 / 100);
secondary[4] = Math.ceil(secondary[4]);
vce[4] = (vce[0] - vce[0] * 75 / 100);
vce[4] = Math.floor(vce[4]);
}
var newArray = [];
newArray.push({'prep':prep}); //prep array = 733,697
newArray.push({'primary':primary}); //primary array = 792,753
newArray.push({'secondary':secondary}); //secondary array = 879,835
newArray.push({'vce':vce}); //vce array = 1108,1052
return newArray;
}
Essentially I've given an example in the .push sections at the bottom. I then call my function by doing this:
var fees = getFees(2);
alert(fees);
Which alerts this:
[object Object],[object Object],[object Object],[object Object]
If I do:
alert(fees.toSource());
I get this:
[{prep:[733, 697]}, {primary:[792, 753]}, {secondary:[879, 835]}, {vce:[1108, 1052]}]
What I need to be able to do is get the number from any of the items (prep/primary/secondary/vce) eg..
fees.prep[0];
fees.primary[1];
But when I try that, I get this error:
TypeError: fees.prep is undefined
What am I missing?? Any help would be greatly appreciated!! :)
you need to access like this
fees[0].prep[0];
Related
I am trying to make a function that randomizes the placement of some clouds I imported, but the problem is, it generates the amount of clouds, but in the same position! I randomized the position, but when the code runs I have like 10 clouds in the same position. What can I do? this is the code:
loader.load('/clouds/clouds1/scene.gltf', function (clouds1) {
var clouds1array = []
function addClouds1(){
for (var i = 1; i < 10; i++) {
const clouds1Mesh = clouds1.scene
const clouds1Position = clouds1Mesh.position
clouds1Position.x = Math.random() * 10
clouds1Position.y = Math.random() * 10
clouds1Position.z = (Math.random() - 0.5 ) * 300
clouds1Mesh.scale.setX(0.05)
clouds1Mesh.scale.setY(0.05)
clouds1Mesh.scale.setZ(0.05)
scene.add(clouds1Mesh)
clouds1array.push(clouds1Mesh)
}
}
addClouds1()
})
edit: clouds1.scene structure is this:
I don't know why it has this amount of children, I tried to solve with the answer, but it still does not work. The 3rd child in the end contains the mesh, and I tried using that for it to work, but it says that I cannot use it in the scene.add() function
edit: I solved the problem! I just had to put the for loop outside the load function
for(let i = 0; i < 30; i+= 3)
loader.load('/clouds/clouds1/scene.gltf', function (clouds1) {
const cloud = clouds1.scene
const child1 = clouds1.scene.children[0].children[0].children[0].children[2].children[0]
child1.material = new THREE.MeshStandardMaterial({ emissive: 'white', emissiveIntensity: 0.5})
cloud.scale.set(0.05, 0.05, 0.05)
cloud.position.x = (Math.random() - 0.5) * 500
cloud.position.y = (Math.random() + ((Math.random() + 20 ) + 70))
cloud.position.z = (Math.random() - 1) * 500
cloud.rotation.x = Math.random()
cloud.rotation.y = Math.random()
cloud.rotation.z = Math.random()
scene.add(cloud)
})
The GLTFLoader results, which you have as clouds1 is a generic object, from which you properly extract clouds1.scene. However, clouds1.scene is also a single Scene object. If you have 10 clouds in the GLTF model you loaded, then clouds1.scene will have 10 children, and you will need to loop through them like this:
loader.load('/clouds/clouds1/scene.gltf', function (clouds1) {
var clouds1array = []
const clouds1Children = clouds1.scene.children
for (var i = 1; i < 10; i++) {
const clouds1Mesh = clouds1Children[i]
const clouds1Position = clouds1Mesh.position
clouds1Position.x = Math.random() * 10
clouds1Position.y = Math.random() * 10
clouds1Position.z = (Math.random() - 0.5 ) * 300
clouds1Mesh.scale.setX(0.05)
clouds1Mesh.scale.setY(0.05)
clouds1Mesh.scale.setZ(0.05)
scene.add(clouds1Mesh)
clouds1array.push(clouds1Mesh)
}
})
I have a formula that calculates the experience based on a certain level and another that calculates the level based on the given experience.
But the second function does not return the expected value.
const levels = 40;
const xp_for_first_level = 1000;
const xp_for_last_level = 1000000;
const getExperience = level => {
const B = Math.log(xp_for_last_level / xp_for_first_level) / (levels - 1);
const A = xp_for_first_level / (Math.exp(B) - 1.0);
const old_xp = Math.round(A * Math.exp(B * (level - 1)));
const new_xp = Math.round(A * Math.exp(B * level));
return new_xp - old_xp;
};
const getLevel = experience => {
const B = Math.log(xp_for_last_level / xp_for_first_level) / (levels - 1);
const A = xp_for_first_level / (Math.exp(B) - 1.0);
return Math.ceil(Math.log(experience / A) / B);
};
console.log(getLevel(xp_for_first_level)); // -9
console.log(getLevel(xp_for_last_level)); // 30
Expected result 1 and 40, but returns -9 and 30.
Can anyone help?
Anyone enlighten me, please?
I think the formula should change, i tried with this and it given correct return.
const getLevel = experience => {
const B = Math.log(xp_for_last_level / xp_for_first_level) / (levels + 1);
const A = xp_for_first_level - 1;
return Math.ceil(Math.log(experience / A) / B);
};
In my project, I want my array to increase in size every 5 seconds. I tried to use setInterval to call a function to do this, but it resets my array every 5 seconds with an increased amount rather than naturally growing. Is there a way to increase the amount without having to reset the array each time?
These are the functions I am using to call my "plants":
var myPlants = new Array();
var plantSpawn = 0;
function createPlants() {
reproducePlants();
setInterval(reproducePlants, 5000);
}
function reproducePlants() {
plantSpawn += 5;
for(var i=0; i<plantSpawn; i++){
var rr = Math.round(Math.random() * 150);
var gg = Math.round(Math.random() * 255);
var bb = Math.round(Math.random() * 150);
var plant = new Object();
plant.x = Math.random() * canvas.width;
plant.y = Math.random() * canvas.height;
plant.rad = 2;
plant.skin = 'rgba('+rr+','+gg+','+bb+', 1)';
myPlants[i] = plant;
}
}
You are explicitly reseting all the values of the array when you do this:
for(var i=0; i < plantSpawn; i++){... myPlants[i] = plant; ...}
Note that plantSpawn will hold the new array size, so you are looping over all the old indexes plus the new ones and re-assigning the values on they.
So, instead you can add 5 new elements to the array with Array.push() this way:
var myPlants = new Array();
var plantsInc = 5;
function createPlants()
{
reproducePlants();
setInterval(reproducePlants, 5000);
}
function reproducePlants()
{
// Push N new plants on the array.
for (var i = 0; i < plantsInc; i++)
{
var rr = Math.round(Math.random() * 150);
var gg = Math.round(Math.random() * 255);
var bb = Math.round(Math.random() * 150);
var plant = new Object();
plant.x = Math.random() * canvas.width;
plant.y = Math.random() * canvas.height;
plant.rad = 2;
plant.skin = 'rgba('+rr+','+gg+','+bb+', 1)';
// Push a new plant on the array.
myPlants.push(plant);
}
}
And as a suggestion, you can even wrap the logic to create a new plant inside a method, like this:
var myPlants = new Array();
var plantsInc = 5;
function createPlants()
{
reproducePlants();
setInterval(reproducePlants, 5000);
}
function createPlant()
{
var rr = Math.round(Math.random() * 150);
var gg = Math.round(Math.random() * 255);
var bb = Math.round(Math.random() * 150);
var plant = new Object();
plant.x = Math.random() * canvas.width;
plant.y = Math.random() * canvas.height;
plant.rad = 2;
plant.skin = 'rgba('+rr+','+gg+','+bb+', 1)';
return plant;
}
function reproducePlants()
{
// Push N new plants on the array.
for (var i = 0; i < plantsInc; i++)
{
myPlants.push(createPlant());
}
}
You are overriding all the existing values so instead of using myPlants[i] = plant; use myPlants.push(plant)
You'll want to modify your function to add a plant onto an existing array, rather than looping over your array and assigning it new values.
if you want to add a new value to your array every 5 seconds, something like this should work:
var myPlants = new Array();
var plantSpawn = 0;
function createPlants() {
reproducePlants();
setInterval(reproducePlants, 5000);
}
function reproducePlants() {
var rr = Math.round(Math.random() * 150);
var gg = Math.round(Math.random() * 255);
var bb = Math.round(Math.random() * 150);
var plant = new Object();
plant.x = Math.random() * canvas.width;
plant.y = Math.random() * canvas.height;
plant.rad = 2;
plant.skin = 'rgba('+rr+','+gg+','+bb+', 1)';
myPlants.push(plant);
}
This just adds a new plant to the end of your array rather than assigning new values to your current array each time you call your function. From here you should be able to put that into a for loop if you wanted to add more plants than 1 at a time. each iteration of the loop will only add a single plan to your array.
I am using javascript to calculate the tax of the products. I am using the following script for it:
<script>
function getTotalTax() {
var numVal1 = Number(document.getElementById("price1").value);
var numVal6 = Number(document.getElementById("tax1").value);
var numVal2 = Number(document.getElementById("price2").value);
var numVal7 = Number(document.getElementById("tax2").value);
var totalValue2 = (numVal1 / '100' * numVal6) + (numVal2 / '100' * numVal7);
document.getElementById("total_tax").value = totalValue2.toFixed(2);
}
</script>
Now I want to extend this script. I added three more textboxes. In the first textbox I want to add only the calculation for where the tax='10', in the second I want to add only where the tax='20' and in the third I want to add only where the tax='25'.
I need to make a script like this:
var totalValue3 = IF numval='10'(numVal1 / '100' * numVal6) + IF numval='10'(numVal2 / '100' * numVal7)
var totalValue3 = IF numval='20'(numVal1 / '100' * numVal6) + IF numval='20'(numVal2 / '100' * numVal7)
var totalValue3 = IF numval='25'(numVal1 / '100' * numVal6) + IF numval='25'(numVal2 / '100' * numVal7)
But this script is not working.
Does someone know how I can make a calculation for this?
JSfiddle:
https://jsfiddle.net/mm2fLne9/
Update:
I think this is the code you are looking for.
https://jsfiddle.net/mm2fLne9/8/
function getTotalTax() {
var numVal1 = Number(document.getElementById("price1").value);
var numVal6 = Number(document.getElementById("tax1").value);
var numVal2 = Number(document.getElementById("price2").value);
var numVal7 = Number(document.getElementById("tax2").value);
var totalValue2 = (numVal1 / '100' * numVal6) + (numVal2 / '100' * numVal7);
document.getElementById("total_tax").value = totalValue2.toFixed(2);
var totalValue3 = (numVal6 ==10 ?(numVal1 / 100 * numVal6) :0) + (numVal7 ==10 ?(numVal2 / 100 * numVal7):0);
document.getElementById("ttax1").value = totalValue3.toFixed(2);
var totalValue4 = (numVal6 ==20 ?(numVal1 / 100 * numVal6) :0) + (numVal7 ==20 ?(numVal2 / 100 * numVal7):0);
document.getElementById("ttax2").value = totalValue4.toFixed(2);
}
The third-party is feeding me 10 percent as the number 10.
In my code how do I move the decimal 2 places over for the variable itemDiscountPercent?
if (boolActive)
{
itemDiscountPercent = Math.percent(ogCookie.products[i].discount_percent);
itemPrice = ogCookie.products[i].price;
itemQty = ogCookie.products[i].quantity;
if (itemQty > 1)
{
itemDiscountPercent = itemDiscountPercent * itemQty;
}
priceDiscount = itemPrice * itemDiscountPercent;
alert(itemDiscountPercent);
alert(itemPrice);
alert(priceDiscount);
}
So instead of getting 299.8 for a line item with quantity 2, I need it to be 2.99 so I can subtract it later in the code.
divide by 100.
var dec = itemDiscountPercent / 100;
if (boolActive)
{
itemDiscountPercent = ogCookie.products[i].discount_percent;
itemPrice = ogCookie.products[i].price;
itemQty = ogCookie.products[i].quantity;
//priceDiscount = itemPrice * itemQty * itemDiscountPercent / 100;
priceDiscount = Math.round(itemPrice * itemQty * itemDiscountPercent - 0.5) / 100;
alert(itemDiscountPercent);
alert(itemPrice);
alert(priceDiscount);
}