Can ASP.NET Core WebAPI Actions have void return types?

ASP.NET Core WebAPI actions CAN have void return types. The framework returns an EmptyResult for such actions with a default 200 (OK) status Code.

This can be particularly observed in the cases of HttpDelete operations where no return is expected by the client in most cases.

Explain Map, Reduce and Filter functions.

Map, Reduce and Filter are three functions which operate on a given array of values.

  • Map function works like a projection, where we can project each value from the input set into some transformed output.
  • It takes a callback function which has two parameters - the current value and the index of the current value and expects a return value from the callback.
let arr = [1, 2, 3, 4, 5];
let result2 = arr.map(function (current, index) {
    return current % 2;
});
// output:
[ 1, 0, 1, 0, 1 ]
  • Reduce function works like an aggregate function, where we can create an aggregation from a given input set.
  • It takes a callback function which has three parameters - the previous value, the current value and the current index and expects a return value from the callback, which is applied onto the next array element iteration.
let result1 = arr.reduce(function (prev, current) {
    console.log(`${prev}, ${current}`);
    return prev + current;
});
// output:
1, 2
3, 3
6, 4
10, 5
result1: 15
  • Filter function is an extraction function where the input array is filtered based on a condition.
  • It takes a callback function which has two parameters - the current value and the current index and expects a boolean return value which represents whether the current element satisfies the condition or not.
let result3 = arr.filter(function (current, index) {
    return current % 2 === 0;
});
// output:
[ 2, 4 ]

How can you extract the id attribute of all the elements in the document with JavaScript?

We can do it in three steps.

  1. Pull all the elements in the document irrespective of their Tag.
  2. Loop through all the Elements and see if the element has an attribute id
  3. If exists collect the attribute value into the output array
<!DOCTYPE html>
<html>
<body id="myBody">
    <div id="mainContainer" class="container-fluid">
        <div id="firstRow" class="row">
            <div id="fullCol" class="col-md-12">
                <h1 id="headingText">Hello World!</h1>
                <p id="subHeadingText">
                This is some sample text you're viewing on the Page.</p>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        (function () {
            let elements = document.getElementsByTagName("*");
            let ids = [];
            for (let i = 0; i < elements.length; i++) {
                let element = elements[i];
                if (element.id) {
                    ids.push(element.id);
                }
            }
            console.log(JSON.stringify(ids));
        })();
    </script>
</body>
</html>

output:
["myBody","mainContainer","firstRow","fullCol","headingText","subHeadingText"]

How can you restrict the number of instances that can be created on a class?

  • To keep a track on the number of instances created on a class, you can maintain a static counter that is incremented each time the constructor is called. By principle, the constructor is called exactly once whenever a new instance is created.
  • If the counter reaches over a limit, you can throw an Exception which breaks the instantiation.
    class MyClass2
    {
        private static int _counter = 0;
        public MyClass2()
        {
            if (_counter > 2)
            {
                throw new Exception("Instances created beyond limit");
            }
            else
            {
                ++_counter;
            }
        }

        public void ShowCounter()
        {
            Console.WriteLine($"Counter is at {_counter}");
        }
    }
    
    
    MyClass2 c;
    for (int i = 0; i < 5; i++)
    {
        c = new MyClass2();
        c.ShowCounter();
    }
    
    output:
    Counter is at 1
    Counter is at 2
    Counter is at 3
    Unhandled exception. System.Exception: Instances created beyond limit