As said By AgaveJoe I will try to capture the validation during Submit.
@page "/Range"
@using System.ComponentModel.DataAnnotations;
<PageTitle>Probability</PageTitle>
<ul>
@foreach (var todo in todos)
{
<li>
<input type="checkbox" @bind="todo.IsDone" />
<input readonly @bind="todo.Title" />
</li>
}
</ul
<input placeholder="Something todo" @bind="newTodo" />
<button @onclick="AddTodo">Add</button> <br>
<button @onclick="DeleteTodo">Delete</button>
<div>
Ascending Order
</div>
<ol>
@foreach (var todo in listsort)
{
<li>
<input readonly @bind="todo.Title" />
</li>
}
</ol>
<button @onclick="Submit">Perform Calc</button>
<div>
Range:
</div>
<div>
@ran
</div>
<br />
<div>
Count:
</div>
<div>
@rancnt
</div>
<br />
<div>
Sum:
</div>
<div>
@meansum
</div>
<br />
<div>
Mean:
</div>
<div>
@calcmean
</div>
<br />
<div>
Mode(s)
</div>
<div>
<ul>
@for (int i = 0; i < myNum.Length; i++)
{
<li>
@if(display=="Y")
{
@myNum[i]
}
</li>
}
</ul>
</div>
<br />
<div>
Median
</div>
<div>
@calcmedian
</div>
<div>
@errmsg
</div>
@code {
public class TodoItem
{
public int Title { get; set; }
public bool IsDone { get; set; }
}
public class Groups
{
public int Title { get; set; }
}
public class Summary
{
public int Title { get; set; }
public int cnts { get; set; }
}
private List<TodoItem> todos = new();
private List<TodoItem> listsort = new();
private int newTodo; int rancnt; int meansum;
private string errmsg;private string skip; private int max;private int ran; private decimal calcmean; private decimal calcmedian; int median = 0; private int[] myNum = { 0 } ;
private List<Groups> modegroups = new();
private List<Groups> tempgroups = new(); private string display = "N"; private int[] medarr;
private List<Summary> sumgroups = new();
protected override void OnInitialized()
{
}
private void DeleteTodo()
{
display = "N";
ran = 0;
rancnt = 0;meansum = 0; calcmean = 0;calcmedian = 0;
int rem = todos.Count((s => s.IsDone == true));
if (rem > 0)
{
var toRemove = todos.Where(todos => todos.IsDone == true).ToList();
foreach (var s in toRemove)
{
todos.Remove(s);
}
}
listsort = todos.OrderBy(p => p.Title).ToList();
}
private void AddTodo()
{ display = "N";
myNum[0] = 0;
ran = 0;
rancnt = 0;meansum = 0;calcmean = 0;calcmedian = 0;
errmsg = "";
skip = "N";
errmsg = "";
if( (newTodo < 0) || (newTodo > 100))
{ skip = "Y";
errmsg = "Enter number between 0 and 100";}
int cnt = todos.Count();
if (cnt>20)
{ skip = "Y";
errmsg = "Cannot enter number more than 20 numbers";}
if(skip=="N")
{
todos.Add(new TodoItem { Title = newTodo });
}
newTodo = 0;
listsort = todos.OrderBy(p => p.Title).ToList();
}
private void Submit()
{ display = "Y";
meansum = todos.Sum(p => p.Title) ;
rancnt = todos.Count();
if (rancnt >0)
{
ran = todos.Max(p => p.Title)-todos.Min(p => p.Title) ;
calcmean = Math.Round( Decimal.Divide(meansum , rancnt),2) ;
//
var tempgroups =listsort.GroupBy(n => n.Title)
.Select(n => new
{
Title = n.Key,
TitleCount = n.Count()
})
.OrderBy(n => n.Title);
// sumgroups = tempgroups;
int groupmax = tempgroups.Max(p => p. TitleCount );
var modegroup =tempgroups.Where(groups => groups.TitleCount == groupmax).Select(n => n.Title ).ToList();
myNum = modegroup.ToArray();
var medgroup =listsort.Select(n => n.Title ).ToList();
medarr = medgroup.ToArray();
int size = medarr.Length;
int mid = size/2;
// int median = 0;
if ((size % 2) != 0){
calcmedian = medarr[mid];
}
else {
int val1 = medarr[mid - 1];
int val2 = medarr[mid];
int val3 = val1 + val2;
calcmedian = Decimal.Divide(val3,2);
calcmedian = Math.Round(calcmedian, 2);
}
}}
}