0

I have this JavaScript Function that works:

var char = 10;
    function grc(len) {
        if (len < 1) {
            return 0;
        }
        if (len == 1) {
            return Math.pow(char, 5);
        }
        return Math.pow(char, len - 1) + grc(len - 1);
    }
        console.log(grc(5));

Non-working Javascript?

var char = 10;
function grc(len) {
    if (len < 1) {
        return 0;
    }
    if (len == 1) {
        return Math.pow(char, len);
    }
    return Math.pow(char, len - 1) + grc(len - 1);
}
    console.log(grc(5));

And I need it in VBA, I gave it a try but I'm not good in VBA:

Function GRC(length)
If (length < 1) Then
    GRC = 0
  End If
        
If (length = 1) Then
    GRC = char ^ length
  End If
  
  GRC = char ^ (length - 1) + GRC(length - 1)
    
  End Function

But it doesn't Work, I'm trying to write this in excel so not really getting any useful errors. its supposed to work like this:

the GRC function would need to know the "characters" variable and take in the "length" variable, in java-script I just created the "characters" variable above the function so the function could call on to it. But in this scenario I'm using VBA and in excel which I don't know if there is a way to get a variable from the spreadsheet without taking it in to the function. as far as the expected output goes if: char = "10", "length" = "5" then the output would be: 111,110 if: char = "52", "length" = "3" then the output would be: 143,364

the algorithm that it's using is from https://www.grc.com/haystack.htm

7
  • Why are you saying it does not work? Remember to add test data, expected output, actual output and any error you are getting while using your code. Don't hesitate on looking thru the tour or in the help to be sure about how to ask questions Commented Oct 14, 2020 at 10:58
  • Java is not JavaScript. Assuming your first paragraph is JavaScript, it still is invalid. Variable names cannot have hyphens, and number-of-characters makes no sense. This looks more like pseudo code, but even then it has undefined references. Commented Oct 14, 2020 at 11:24
  • I used hyphens just because it was an example. But I fixed it Commented Oct 14, 2020 at 11:27
  • What is numberofcharacters? It is undefined. Commented Oct 14, 2020 at 11:27
  • It's usually defined before the function Commented Oct 14, 2020 at 11:30

1 Answer 1

1

The main issue is that the function doesn't return immediately when you assign a value to the function variable, so GRC = 0 is not equivalent with return 0 in the JavaScript version.

Instead, use an Else If chain:

numChars = 10

Function GRC(length)
    If (length < 1) Then    
        GRC = 0
    ElseIf (length = 1) Then
        GRC = numChars ^ length
    Else
        GRC = numChars ^ (length - 1) + GRC(length - 1)
    End If
End Function
Sign up to request clarification or add additional context in comments.

13 Comments

not sure why but this isn't giving the expected output if: char = "10", "length" = "5" then the output should be: 111,110 but its 11,120
its give an output though so that's better than what I had before
Your JavaScript version is also returning 11120. See in this fiddle (open the little console widget to see the output of 11120). So the VBA version is doing what the JS version is doing.
OK you're right it does and it's extremely confusing because the java script in this case it was in HTML. Still gives the correct output and the code is identical and I can't track down why it's giving different results Here: bookmarkletserver.000webhostapp.com/github/…
Your question was to convert a piece of JS to VBA. Asking us to dissect an external website's functioning is another question and is one that is off topic for Stack Overflow.
|

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.