Views/Home/RecusionIndex.cshtml



@{
    ViewBag.Title = "Recursion Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<div class="col-sm-12">

    <div class="row col-sm-12 text-center"><h4 style="color:red;">(click on headings for code snippets)</h4></div>

    <div class="row col-sm-12">
        <div class="col-sm-4"></div>
        <div id="dvRecursion" data-toggle="modal" data-target="#modalRecursion" class="row text-center col-sm-4"><h1>Recursion</h1></div>
        <div class="col-sm-4"></div>
    </div>

    <div class="row  paddingBottom10 fontLargeBlueComicBold" style="font-size: large;">
        <center>
            <input id="inpNum" name="inpNum" type="number" 
                   min="1" max="10" placeholder="1" 
                   autofocus/>
            <button id="btnRefactor" name="btnRefactor" class="btn btn-info btn-sm"
                    type="button" onclick="getFactorial();">
                Submit
            </button>
            <label id="lblRecurve"></label>
               @ViewBag.Recurve
        </center>
    </div>

    <div class="col-sm-12" style="height:20px;"></div>

    <div class="row">
        <div class="col-sm-12" style="border:2px solid fuchsia;">
            <div id="dvImageFiles" data-toggle="modal" data-target="#modal2ndDelegate" class="row text-center"><h2>List of pics in Images Drectory</h2></div>
            @if (ViewBag.ImageFiles != null)
            {
                foreach (var item in ViewBag.ImageFiles)
                {
                    <div class="row" style="padding-left:25px;">
                        @item
                    </div>
                }
            }
            else
            { <dv class="row" style="padding-left:25px;">ViewBag.ImageFiles is null</dv>}
        </div>
    </div>
    <div class="row" style="height:40px;"></div>
    <div class="row">
        <div class="col-sm-12" style="border:2px solid fuchsia;">
            <div id="dvImageFiles" data-toggle="modal" data-target="#modal2ndDelegate" class="row text-center"><h2>List of pics in Images Drectory</h2></div>
        </div>
    </div>

    <div class="row" style="height:40px;"></div>

    @*Recustion Modal*@
    <div id="modalRecursion" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        ×
                    </button>
                    <h4 class="modal-title text-center">
                        Recursion snippets
                    </h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form" method="post">
                        <div class="form-group" style="font-weight:bold;">
                            <pre>
    private void findFiles(string path, IList<string>
    fileList)
    {
    foreach(string fileName in Directory.GetFiles(path))
    {
    int loc = 0;
    loc = fileName.IndexOf("Images");
    string f = fileName.Substring(loc);
    fileList.Add(f);
    }

    foreach(string directory in Directory.GetDirectories(path))
    {
    findFiles(directory, fileList);
    }

    ViewBag.ImageFiles = fileList;
    }

    public string getFactorial(int inpNum)
    {
    return getFactorialByRecursion(inpNum).ToString(); ;
    }

    public static double getFactorialByRecursion(int inpNum)
    {
    if (inpNum == 0)
    return 1;
    return inpNum * getFactorialByRecursion(inpNum - 1);//Recursive call

    }

    //------------------------------------------------------------------
    //Recursion
    #region recursion
    public ActionResult RecursionIndex()
    {
    String d = HostingEnvironment.MapPath(@"/Areas/AlbacoreComics/Images");

    Listltstring> fileList = new Listltstring>();
    findFiles(d, fileList);

    //ViewBag.Recurve = getFactorialByRecursion(5).ToString();
    //ViewBag.RecurveString = "factorial of 5: ";
    return View();
    }

    //https://www.c-sharpcorner.com/UploadFile/955025/C-Sharpinterviewquestionpart5what-is-real-time-examplewhere-rec/
    private void findFiles(string path, IListltstring> fileList)
    {

    foreach (string fileName in Directory.GetFiles(path))
    {
    int loc = 0;
    loc = fileName.IndexOf("Images");
    string f = fileName.Substring(loc);
    fileList.Add(f);
    }

    foreach (string directory in Directory.GetDirectories(path))
    {
    //recursive call to parent method
    findFiles(directory, fileList);
    }

    ViewBag.ImageFiles = fileList;
    }
    public string getFactorial(int inpNum)
    {
    return getFactorialByRecursion(inpNum).ToString();
    }

    //https://www.c-sharpcorner.com/UploadFile/955025/C-Sharp-interview-questions-part4what-is-a-recursive-function-in/
    public static double getFactorialByRecursion(int inpNum)
    {
    if (inpNum == 0)
    return 1;
    return inpNum * getFactorialByRecursion(inpNum - 1);//Recursive call

    }

    #endregion
    //----------------
</pre>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">
                        Close
                    </button>
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript">

        function getFactorial() {

            var url = '@Url.Action("getFactorial")';
            var inNum = $('#inpNum').val();
            var getJsonUrl = url + inNum;
            var text = " (";

            $.ajax({
                dataType: "text",
                url: url,
                data: { inpNum: inNum },
                success: function (data) {
                    for (let i = 1; i < inNum; i++) {
                        text += i + " x ";
                    }
                    text += inNum + ") ";

                    document.getElementById("lblRecurve").innerHTML =
                        "   factorial of " + inNum + text + " = " + data ;
                },
                error: function (e) {
                    alert('127 error occured ' + e);
                }
            });


            //$.getJSON(
            //    getJsonUrl,
            //    inpNum,
            //    function (data) {
            //        alert('120 getjson success ' + data);
            //        //$("#lblRecurve").html("");
            //    });
            //    //.done(function (data) {
            //    //    alert('120 getjson success ' + data);
            //    //    //$("#lblRecurve").html("");
            //    //    //$.each(data, function (key, item) {
            //    //    //    // Add a list item for the comic.
            //    //    //    $('<li>', { text: formatItem(item) }).appendTo($('#foundComics'));
            //    //    //});
            //    //})
            //    //.fail(function (jqXHR, textStatus, err) {
            //    //    alert('129 getjson jq= ' + jqXHR + ' stat=' + textStatus + ' Error: ' + err + ' uri=' + getJsonUrl);
            //    //    //*$('#comic').text('Error: ' + err);*/

            //    //    /* 129 getjson jq= [object Object] stat=error Error: Not Found uri=/Home/getFactorialByRecursion2/3*/
            //    //});


        }

        //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
            function type(obj, showFullClass) {

                // get toPrototypeString() of obj (handles all types)
                if (showFullClass && typeof obj === "object") {
                    return Object.prototype.toString.call(obj);
                }
                if (obj == null) { return (obj + '').toLowerCase(); } // implicit toString() conversion

                var deepType = Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
                if (deepType === 'generatorfunction') { return 'function' }

                // Prevent overspecificity (for example, [object HTMLDivElement], etc).
                // Account for functionish Regexp (Android <=2.3), functionish <object> element (Chrome <=57, Firefox <=52), etc.
                // String.prototype.match is universally supported.

                return deepType.match(/^(array|bigint|date|error|function|generator|regexp|symbol)$/) ? deepType :
                    (typeof obj === 'object' || typeof obj === 'function') ? 'object' : typeof obj;
            }



    </script>
</div>


      
copyright © MvcBootstrap 2015