1

I am trying to convert some VBA excel code to JS, but even I was sure of me, the code does not work properly. Here are the snippets which cause me problems :

VBA: Tri1 = Tri1 + 2 ^ (2 - Tour)
JS: tri1 = tri1 + Math.pow(2, (2 - tour));

And:

VBA: If (y And (2 ^ (5 - Tour))) <> (Tri2 And 2 ^ (5 - Tour)) Then
JS: if ((y && Math.pow(2, 5 - tour)) != (tri2 && Math.pow(2, 5 - tour)))

And :

VBA: If (Tri1 And 2 ^ (2 - Tour)) = 0 Then
JS: if ((tri1 && Math.pow(2, 2 - tour)) == 0)

tri1, tri2 and y are all integers.

I have good understanding of JS, but none in VBA. So I googled a lot before already.

Thanks !

2
  • 2
    Can you clarify what exactly is not working? What are the inputs, what you expect, what you actually get? Commented Dec 16, 2015 at 13:12
  • I expect the same results that the VBA ones... I can't clarify much, cause I don't know the outputs in VBA line by line. The inputs are for example : y = 0 to 7; tour = 1; tri1 = 0 (first iteration); tri = 0 (first iteration); But is my "translation" is correct ? Commented Dec 16, 2015 at 13:19

1 Answer 1

2

This is not a complete answer, but I think that information could get you to the goal. Because you are just trying to translate the words from vba to js. But js is not vba and js has some special things.

First: Floating numbers

A floating number in js is not always that like it looks like. That question here will give you more information about that topic: How to deal with floating point number precision in JavaScript?

Because of the code snipets I don't know if one of your variables is a floating number. But if, you should know about that problem.

Second: Comparison

The way JS is handling comparisons is a litle bit different to some other languages. Some helpful links about that topic:

Specially the comparisons in your code are not very clear. For example:

var a = 10;
var b = 0;
document.write((a && b) == 0); // is true
document.write(a && b) // is 0
var a = 10;
var b = 11;
document.write((a && b) == 0); // is false
document.write(a && b) // is 11

Is that really the behaviour you want to have? So try to simplify your comparisons and your code could work.

Sign up to request clarification or add additional context in comments.

1 Comment

After struggling with this a few hours, I found my mistake : "And" in VBA, in these case is "&" in JS, the binary operator... Thanks anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.