Arithmetic operators in JADE - node.js

I'm using Jade template engine with NOde.js.
I have two variables:
a = 0.0378
b = 0.1545
in Jade I do:
- var result = a + b*2
and I get a very strange number when I do #{result}
0.03780.309
It seems to concatenate the numbers as strings.
Could someone tell me how can I use arithmetic operators in Jade?
Thanks

Are you sure that a (and also b) is a number and not a string?
If it is a string you will need to to convert it to a number via parseFloat:
- var result = parseFloat(a)+parseFloat(b)*2

h2 Jadeçš„ operation
- var a = 8
- var b = 2
p #{+a+b}
p #{+a-b}
p #{+a*b}
p #{+a/b}

Related

Manipulating numerical document value from findById query

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.

In my node application, why the same thing gives the different outputs?

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 :)

How to get int from float in picat?

I try to to read a line as string from console (stdin) in picat and get its half:
main =>
L = read_line(),
B = L.length/2,
S = L.slice(1,B),
println(S).
crashes with error(integer_expected(2.0),slice)
when int used instead of B - no crash. So how to turn B into integer?
you could use built-in function such as floor, round or ceiling from math module (more functions here). So you could modify your code like this:
main =>
L = read_line(),
B = round(L.length/2),
S = L.slice(1,B),
println(S).
Try either using integer(..) function to convert L.length/2 to integer or use to_integer() function....should do it for you.
type inference plays an essential role in functional evaluation. (/ /2) it's a floating point arithmetic operator, but slice/2 expects an integer. So you should instead use (// /2).
Picat> L=read_line(),println(L.slice(1,L.length//2)).
123456789
1234
L = ['1','2','3','4','5','6','7','8','9']
yes

How to use 'aux' keyword in Haskell

Okay, I've looked on about 4-5 websites that offered to teach Haskell and not one of them explained the keyword aux. They just started using it. I've only really studied Java and C (never saw it in either if it exists), and I've never really encountered it before this class that I'm taking on Haskell. All I can really tell is that it provides the utility to create and store a value within a function. So what exactly does it do and how is it properly used and formatted? In particular, could you explain its use while recursing? I don't think that its use is any different, but just to make sure I thought I would ask.
There is no keyword aux, my guess is this is just the name they used for a local function.
Just like you can define top-level values:
myValue = 4
or top-level functions:
myFunction x = 2 * x
you can similarly define local values:
myValue =
let myLocalValue = 3 in
myLocalValue + 1
-- or equivalently:
myValue = myLocalValue + 1
where myLocalValue = 3
or a local function:
myValue =
let myLocalFunction x = 2 * x in
myLocalFunction 2
-- or equivalently:
myValue = myLocalFunction 2
where myLocalFunction x = 2 * x
Your teacher simply named the local function aux instead of myLocalFunction.

Multiplying a string in F#

I have a question I am rather unsure about.
My questions is as follows
let myFunc (text:string) (times:int) = ....
What I want this function to do is put the string together as many times as specified by the times parameter.
if input = "check " 3 I want the output string = "check check check"
I have tried with a loop, but couldn't seem to make it work.
Anyone?
Actually the function is already in String module:
let multiply text times = String.replicate times text
To write your own function, an efficient way is using StringBuilder:
open System.Text
let multiply (text: string) times =
let sb = new StringBuilder()
for i in 1..times do
sb.Append(text) |> ignore
sb.ToString()
If you want to remove trailing whitespaces as in your example, you can use Trim() member in String class to do so.
A variation on pad's solution, given that it's just a fold:
let multiply n (text: string) =
(StringBuilder(), {1..n})
||> Seq.fold(fun b _ -> b.Append(text))
|> sprintf "%O"
If you want a pure functional "do-it-yourself" version for F# learning purposes, then something like the following snippet will do:
let myFunc times text =
let rec grow result doMore =
if doMore > 0 then
grow (result + text) (doMore- 1)
else
result
grow "" times
Here is the test:
> myFunc 3 "test";;
val it : string = "testtesttest"
Otherwise you should follow the pointer about the standard F# library function replicate given in pad's answer.
String.replicate already provides the functionality you're looking for.
If for some reason you want the arguments reversed, you can do it as follows:
(* A general function you should add to your utilities *)
let flip f a b = f b a
let myFunc = flip String.replicate
In a simple recursive fashion:
let rec dupn = function
|s,1 -> s
|s,n -> s ^ dupn(s, n-1)

Resources