I want to read the value "-2.5" in variable "x1", and the value "0.4" in other variable called "y1". And the same for the line below: read "12.1" in a variable "x2" and read "7.3" in a variable "y2"
var lines = [
"-2.5 0.4",
"12.1 7.3"
];
var x1 = parseFloat(lines[0]);
var y1 = jQuery(x1).next();
var x2 = parseFloat(lines[1]);
var y2 = jQuery(x2).next();
console.log(x1);
console.log(y1);
console.log(x2);
console.log(y2);
this is the problem i'm solving
and the code i've made so far, but not accepting "Wrong Answer 85%"
Uncaught ReferenceError: jQuery is not defined
Your error can be solved by investigating whether Jquery is loaded or not, but it won't give you the correct result with your current code.
You can do this:
var lines = [
"-2.5 0.4",
"12.1 7.3"
];
var lineParts = lines[0].split(" ");
var x1 = parseFloat(lineParts[0]);
var y1 = parseFloat(lineParts[1]);
lineParts = lines[1].split(" ");
var x2 = parseFloat(lineParts[0]);
var y2 = parseFloat(lineParts[1]);
console.log(x1, y1, x2, y2);
I think you can have a look over this solution for your reference, hope this help
var lines = [
"-2.5 0.4",
"12.1 7.3"
];
// This will convert a string into an array seperated by space (" ")
const generatePosition = string => string.split(" ");
// This will map all our initial values and change them into a full array
const pos = lines.map(string => generatePosition(string));
console.log(pos);
// From now you can freely access the variable the way you want, this is just for sample demo
const a = pos[0][0];
const b = pos[0][1];
console.log(a);
console.log(b);
Can you reformat your lines array so that each value is in its own element? That would make it easier to access each element. Like this:
var lines = [
Array("-2.5","0.4"),
Array("12.1","7.3")
];
Then you could access the values this way:
var x1 = parseFloat(lines[0][0]);
var y1 = parseFloat(lines[0][1]);
var x2 = parseFloat(lines[1][0]);
var y2 = parseFloat(lines[1][1]);
Related
I'm doing some simple math in Javascript, but my equation's result is drastically different than what it should be. The math is:
3.05+(((0.32*0)+3.28)+(1+(0.19*0))*(2.66*1^2))*1;
When I did it out by hand, and then used Wolfram Alpha (https://www.wolframalpha.com/) I get the correct result of 8.99. However, when I use the equation in Javascript I mysteriously get 6.33
The actual equation looks like
VO2move = VO2rest+(((C1*g2)+VO2walkmin)+(1+(C2*g2))*(C3*s2^2))*t2;
but I removed all the variables in an attempt to debug (I thought it might be some error where I needed parseInt)
Here are the whole functions for reference
function calc(){
var temp = 0;
var total = 0;
for(i = 0; i<sArr.length; i++){
total = total + calc2(i);
}
var o = document.getElementById("output");
o.value = total;
}
function calc2(i){
var s = document.getElementById("s"+i);
var g = document.getElementById("g"+i);
var t = document.getElementById("t"+i);
var VO2walkmin = 3.28;
var VO2rest = 3.05;
var C1 = 0.32;
var C2 = 0.19;
var C3 = 2.66;
var Cdecline = 0.73;
var s2 = s.value;
var g2 = g.value;
var t2 = t.value;
var negGrade = g.value;
if(g2 < 0){g2 = 0};
//VO2move = ((C1 * g2)+VO2walkmin)+((1+(C2*g2))*(C3*s2^2)); //ORIGINAL TRANSCRIPTION
//VO2move = VO2rest+(((C1*g2)+VO2walkmin)+(1+(C2*g2))*(C3*s2^2))*t2; // TRANSLATED FROM COPY PASTE
VO2move = 3.05+(((0.32*0)+3.28)+(1+(0.19*0))*(2.66*1^2))*1; // COPY - PASTED FROM EXCEL
return VO2move;
}
Even naked numbers I still get the output of 6.33. I'm totally puzzled, and any help is appreciated.
You need to take the power (exponentiation) operator ** instead of the bitwise XOR operator ^.
console.log(3.05+(((0.32*0)+3.28)+(1+(0.19*0))*(2.66*1**2))*1);
I have array object like this below
var TTP01[2,0,0,0,0,0,4,6,1,4,0,9,1]
If I assign TTP01[0] like this, I will get Output 2. This is working fine.
But I'm getting values separately and I need to assign the Object.
object = TTP;
count =01;
xy = x*y;
I concat like this below
var obj = objname.concat(count, "[", xy, "]");
console.log( obj );
In console log, I'm getting like this TTP01[0].
But want to get output 2
Please help me... Thanks
This will work.
eval(objname + count)[xy]
fullcode:
var TTP01 = [2,0,0,0,0,0,4,6,1,4,0,9,1];
var objname = "TTP";
var count = "01";
var xy = 0;
console.log(eval(objname + count)[xy]); // 2
You can try like this way,
var TTP01 = [2,0,0,0,0,0,4,6,1,4,0,9,1];
var objname = 'TTP';
var count = '01';
xy = 0;
var obj = window[objname + count];
console.log( obj[xy] );
Assign TTP01 to some base object :
var base = {
TTP01: [2,0,0,0,0,0,4,6,1,4,0,9,1]
}
var objname = 'TTP';
var count = '01';
var objStr = objname + count;
var xy = 0;
console.log(base[objStr][xy])
Instead of:
var x = document.forms["myForm"]["dob"].value;
var y = document.forms["myForm"]["age"].value;
var z = document.forms["myForm"]["height"].value;
I wish to type something like this:
var form = document.forms["myForm"];
var x = form.["dob"].value;
var y = form.["dob"].value;
var z = form.["dob"].value;
Yes, try this:
var form = document.forms["myForm"];
var x = form["dob"].value;
var y = form["age"].value;
var z = form["height"].value;
Edit:
sorry for bad explanation so i have:
var x = $("span", this).get( 0 );
var y = x.outerHTML
and it give me:
<span>'some text'</span> but i want just only tag <span></span> with all atributes. My question is how to get it?;p
thanks for any help!
The problem in your code:
var x = $("span", this).get( 0 ); <-- you have parent (even TAG name) and taking first child
var y = x.outerHTML <-- here you're trying to get parent (you eant - TAG name) :%
So, if you know var x = $("span"... <---- this string - you can easily create a string you want
var tag = "span";
var x = $(tag, this).get( 0 );
var y = x.outerHTML;
// the most dumbest way
var result = '<'+tag+'>'+'<'+tag+'/>';
cloneNode() by default does not copy the element content, so you can use it:
var x = $("span", this).get( 0 );
var y = x.cloneNode();
var z = x.outerHTML;
In your function of fiddle it would be something like that:
var x = tur.cloneNode();
console.log(x.outerHTML);
I have made a function but it keeps returning NaN. I can't figure it out why it does this.
function voeruit(){
var invoer1 = 2000;
var invoer2 = 11000;
var invoer3 = 5;
var datum = new Date();
var jaar = datum.getFullYear();
berekenen(invoer1, invoer2, invoer3, jaar);
}
function berekenen(invoer1, invoer2, invoer3, jaar)
{
var leeftijd = jaar - invoer1;
var daling1 = invoer3 / 100;
var daling = 1 - daling;
var totdaling = Math.pow(daling, leeftijd);
var waarde = invoer2 * totdaling;
var uitkomst = waarde;
window.alert(uitkomst);
}
it's a summary where, i think is the problem, so there might be a syntax error.
It's an other language than english, because i'm dutch
Your code has a bug here:
var daling = 1 - daling;
Which should be:
var daling = 1 - daling1;
Looks like you have a typo
var daling1 = invoer3 / 100;
var daling = 1 - daling; //<-- daling1?
Presumably that should be
var daling1 = invoer3 / 100;
var daling = 1 - daling1;
Otherwise you're setting daling to 1 - undefined. Which is indeed not a number.
To able to return something from function you should use
return someValue;
But in your code i could not find something like this. How can you expect function return the value if you dont write return statement?
You are using var daling = 1 - daling; and daling not defined yet, may be you should use daling1
And also I want to suggest to not use variable names which like each other they are always confusing.