I am trying to manipulate the numerical value of a document as so, however when I console.log NewX it gives me NaN rather than the numerical value I am after.
var OldX = await Col.findById(req.params.id, 'X');
var NewX = OldX - 6;
console.log(newX);
I think this is related to the fact that if I just console.log Old X it returns the following
{ X: 2.5, _id: 5baa8b1f4ac3b740248g3005 }
This makes me think that I am trying to subtract 6 from an object rather than from the numerical value 2.5 in this case. Nonetheless I'm not sure how to resolve that.
The solution for this is actually simple and embarrassingly I discovered it moments after asking the question. Changing
var NewX = OldX - 6;
to
var NewX = OldX.X - 6;
gives me access to the numerical value rather than the object.
Related
var b = "pp.specifications.full_specs.";
var c = arr[i];
here the value of arr[i] is Memory
var a = b+c;
console.log(a);
it prints pp.specifications.full_specs.Memory on console
but when I use
console.log(pp.specifications.full_specs.Memory);
then prints an json object as:
{ Series: 'Inspiron',
Model: 'A562103SIN9',
Utility: 'Everyday Use',
OS: 'Windows 10 Home (64-bit)',
Dimensions: '274.73 x 384.9 x 25.44 mm',
Weight: '2.62 Kg',
Warranty: '1 Year Onsite Warranty' }
whenever the value of a contains pp.specifications.full_specs.Memory;
So what is the reason for getting different outputs?
There's a elementary difference between
console.log(pp.specifications.full_specs.Memory);
and
console.log("pp.specifications.full_specs.Memory");
Note the quotes!
So the expression: console.log(pp.specifications.full_specs.Memory); can be read from left to right:
Print to the output value of pp.specifications.full_specs.Memory and this is a value of pp object after taking its property specifications and then its property full_specs etc.
And the "pp.specifications.full_specs.Memory" means only a piece of text.
What is happening in your code should now be clearer:
// B is piece of text
var b = "pp.specifications.full_specs.";
// C is some other value - let's say also textual one
var c = arr[i]
// So b+c will mean add text to some other text
// It's expected that the result of such operations is concatenated text
// So then console.log(b+c) will mean
// Print the concatenated text from b and c
// And it's just plain text
console.log(b+c);
//It's the same as:
console.log("pp.specifications.full_specs.Memory");
// And this one means print some specific object attributes
// which is different operation!
console.log(pp.specifications.full_specs.Memory);
If you want to access objects by text you can do the following:
var d = eval(b+c);
It's pretty dangerous and eval should be avoided but I just wanted to demonstrate the basic idea. Eval executes strings as if they were code.
So string "pp.specifications.full_specs.Memory" will be evaluates (yep!) as value of the actual object.
As The eval can execute anything it should be always avoided and moreover it's superslow!
Instead if you want to access some property of pp basic on some text input you can do:
pp['specifications']['full_specs']['Memory']
As the pp.x notation is equivalent to expression: pp['x']
I hope my answer will help you understand Javascript mechanisms better :)
Here is a Python-like pattern I need to re-create in Chapel.
class Gambler {
var luckyNumbers: [1..0] int;
}
var nums = [13,17,23,71];
var KennyRogers = new Gambler();
KennyRogers.luckyNumbers = for n in nums do n;
writeln(KennyRogers);
Produces the run-time error
Kenny.chpl:8: error: zippered iterations have non-equal lengths
I don't know how many lucky numbers Kenny will have in advance and I can't instantiate Kenny at that time. That is, I have to assign them later. Also, I need to know when to hold them, know when to fold them.
This is a good application of the array.push_back method. To insert lucky numbers one at a time you can do:
for n in nums do
KennyRogers.luckyNumbers.push_back(n);
You can also insert the whole array in a single push_back operation:
KennyRogers.luckyNumbers.push_back(nums);
There are also push_front and insert methods in case you need to put elements at the front or at arbitrary positions in the array.
I don't think I can help on when to hold them or when to fold them.
A way to approach this that simply makes things the right size from the start and avoids resizing/rewriting the array is to establish luckyNumbers in the initializer for Gambler. In order to do this without resizing, you'll need to declare the array's domain and set it in the initializer as well:
class Gambler {
const D: domain(1); // a 1D domain field representing the array's size
var luckyNumbers: [D] int; // declare lucky numbers in terms of that domain
proc init(nums: [?numsD] int) {
D = numsD; // make D a copy of nums's domain; allocates luckyNumbers to the appropriate size
luckyNumbers = nums; // initialize luckyNumbers with nums
super.init(); // mark the initialization of fields as being done
}
}
var nums = [13,17,23,71];
var KennyRogers = new Gambler(nums);
writeln(KennyRogers);
Where am I getting wrong is following understanding?
Scenario 1:
Float[] f1= new Float[2];
Float[] f2= new Float[2];
System.out.println("Values"+f1[0]+","+f2[0]+","+(f1==f2));
Output: 0.0, 0.0, false.
What I understood from this is:
1) Whenever we declare a float type array all the values are initialized to 0.0
2) Though both f1 and f2 have same values, they are pointing to different addressed in Heap.
3) .equals() in Object Class compares address of the values in head along with the values.
HashMap<String, Integer> hashMap = new HashMap();
String str1 = new String("test");
String str2 = new String("test");
System.out.println((str1==str2));
System.out.println((str1.hashCode()==str2.hashCode()));
hashMap.put(str1,1);
hashMap.put(str2,2);
System.out.println(hashMap.get(str1));
Output:false
true
2
Scenario 2
What I understood from this is
1) Two immutable string literals "test" are created. Both of them are pointing to different locations in the memory
2) From Oracle Docs, if values returned by equals is same then the value returned hashcode must be same and not vice versa. So deflection from equals method in second output is justifiable (i.e. in comparison of hashValue). How is hashCode internally working in this case? Isn't it comparing address? Here they are pointing to different locations so they should have different addresses right?
3) They have same hashCode so hashMap is replacing the value of str1 with str2.
Scenario #1
1) Whenever we declare a float type array all the values are initialized to 0.0
Yes that true
2) Though both f1 and f2 have same values, they are pointing to different addressed in Heap.
Yes you are comparing the refrence of two arrays with in the heap.
3) .equals() in Object Class compares address of the values in head along with the values.
yes its same as Answer for #2 above. It compares reference of two objects.
Scenario #2
1) Two immutable string literals "test" are created. Both of them are pointing to different locations in the memory
Yes that's true.
2) From Oracle Docs, if values returned by equals is same then the value returned hashcode must be same and not vice versa. So deflection from equals method in second output is justifiable (i.e. in comparison of hashValue). How is hashCode internally working in this case? Isn't it comparing address? Here they are pointing to different locations so they should have different addresses right?
This is how hashCode method works on String:
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
3) They have same hashCode so hashMap is replacing the value of str1 with str2.
In addition to hashCode, it would check for equality of two objects as well and if both condition satisfies then it will overwrite the value for the key which is same.
There is an issue with enumerating Object.keys() in node.js that I do not understand. With the following code:
Object.prototype.tuple = function() {
var names = Object.keys(this);
console.log("Dump of names:");
console.log(names);
console.log("FOR loop using indexes:");
for (var k = 0; k < names.length; k++)
{
console.log(names[k]);
}
console.log("FOR loop using enumeration:");
for (var z in names)
{
console.log(z);
}
return this;
};
var x = {a:0, b:0, c:0}.tuple();
I get the following results on the console:
Dump of names:
[ 'a', 'b', 'c' ]
FOR loop using indexes:
a
b
c
FOR loop using enumeration:
0
1
2
tuple
Could somebody explain where does an extra "tuple" come from in the second loop? While defined as function in Object.prototype, it is neither an own property of x object, nor included in names array.
I am using node.js version 0.8.20.
The first loop goes over the properties of x (Object.keys() returns only own properties), while the second one goes over the properties or the array names, including the ones up in the prototype chain.
Thanks to Jonathan Lonowski for clarifications.
I think what #Kuba mentioned above is not correct.
Object.keys, Object.getOwnPropertyNames and other similar method would behave different sightly. Their behaviors are related to a property named enumerable.
I am going to dinner with my friends so I can only give you a helpful link illustrating it. So sorry.
https://developer.mozilla.org/en-US/docs/Enumerability_and_ownership_of_properties
I hope this is the right place to post this. I'm a web designer who is doing a site for a client, except for their store location page, which I have contracted out to a Google map developer. The map needs to be searchable by lat and long, in both decimal and degrees/minutes/seconds. The programmer has it set up only for decimal, and says it will be a good deal of effort to make the search use both types.
Being a web designer myself, I know sometimes changes can take a while. However, I want to be sure what the programmer is saying is correct. I know that when I search Google maps in general, it can accept both types of lat and long measurements. I thought therefore, that a map customized to search lat and long via Google maps would also do this. Is that a correct assumption? Or could it potentially involve a good deal of work to make this happen? Should the programmer have assumed it would be searchable in both types of lat long?
Thank you for any feedback!
It does involve some work, but it's not that difficult to do:
function deg_to_dms (deg) {
var d = Math.floor (deg);
var minfloat = (deg-d)*60;
var m = Math.floor(minfloat);
var secfloat = (minfloat-m)*60;
var s = Math.round(secfloat);
// After rounding, the seconds might become 60. These two
// if-tests are not necessary if no rounding is done.
if (s==60) {
m++;
s=0;
}
if (m==60) {
d++;
m=0;
}
return ("" + d + ":" + m + ":" + s);
}