battle programminga forum for elite programmerswith extreme will powerto sharpen theire skills
 
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in
Share | 
 

 JS pascal triangle question

Go down 
AuthorMessage
Admin
Admin
avatar

Posts : 120
Join date : 2011-08-01

PostSubject: JS pascal triangle question   Mon Dec 04, 2017 9:54 am

the following JS code is supposed to output a layer of the pascal triangle :
Code:
function pascal(floor){

 var ar1 = [1,1];
if (floor < 2){return '1 1';}

return pascalPt2(floor,2,ar1);
}


function pascalPt2(floor,at,prevfloor)
{
 var result = ""; var ar2 = [];ar2[0] = 1;

 if (at == floor + 1) {for(var v of prevfloor){result = result + " " + v};return result;}

 else{for (var i=0;i<at - 2;i++){ar2[i+1] = prevfloor[i]+prevfloor[i+1]};ar2[at] = 1;}

 return pascalPt2(floor,at+1,ar2)

}


console.info(pascal(3))

for the input 3 the output is supposed to be : 1 3 3 1
for the input 4 the output is supposed to be : 1 4 6 4 1
but I get : 1 nan nan 1 and 1 nan nan nan 1 instead
here is the working vb.net code :

Code:
Public Class Form1
    Function pascal(ByVal floor As Integer) As String
        Dim ar1 = New Integer() {1, 1}
        If floor < 2 Then
            Return "1 1"


        End If
        Return pascalPt2(floor, 2, ar1)
    End Function
    Function pascalPt2(ByVal floor As Integer, ByVal at As Integer, ByVal prevfloor As Object) As String
        Dim result = ""
        Dim ar2(at) As Integer
        ar2(0) = 1
        If at = floor + 1 Then
            For Each item As Integer In prevfloor
                result &= item.ToString() & " "

            Next
            Return result
        Else
            For index = 0 To at - 2
                ar2(index + 1) = prevfloor(index) + prevfloor(index + 1)
            Next
            ar2(at) = 1
        End If
        Return pascalPt2(floor, at + 1, ar2)
    End Function
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox(pascal(4))
    End Sub
End Class

_________________
kurayami no kagi  _____    
Back to top Go down
View user profile http://yotamarker.forumotion.com
 
JS pascal triangle question
Back to top 
Page 1 of 1
 Similar topics
-
» Answer these questions with song titles
» Curiously Specific Martial Arts Question
» Question for Marc
» Interview: Gamestop Employee Tells Us That Wii Games Will Stop Being Sold On July 1st!
» Stupid Question

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: chit chat-
Jump to: