0

I just started learning programming using dart and JavaScript & I tried doing a simple task assigned for myself for neramclasses.com Best NATA coaching center in trichy

I am collecting the following Data from the user.

  1. 12th Mark obtained,
  2. Maximum Mark of the Exam
  3. NATA Exam attempt 1 Mark
  4. NATA Exam attempt 2 Mark
  5. NATA Exam attempt 3 Mark

Field 1 and 2 are must input fields. 3,4,5 either one of them should be field (All three cannot be left empty).

The task which am confused to perform is

  1. if the user gives only one input of 3,4,5 need to return the single value else
  2. if the user gives two input out of 3,4,5 need to average them and return the value,
  3. if the user gives all the tree values (two greater values) among 3,4,5 has to be taken and return the average of the the two value

kindly give me an idea to perform this using either JavaScript or Dart

Here is the code I tried

    void main() {
  int nata1 = 100;
  int nata2 = 122;
  int nata3 = 98;
  List<int> nata = [nata1, nata2, nata3];

  if (nata.length == 1) {
    print('return one non null value');
  } else if (nata.length == 2) {
    print('return Average of Two');
  } else if (nata.length == 3) {
    print('return Average of greater two score');
  }
}

In the above code how to perform the action instead of print

1 Answers1

1

You can reduce your problem to just two different cases.

  1. Put the answers of 3,4,5 into an array
  2. If size (length) of the array is < 2, return its first element
  3. Else sort the array and return the average of the first (or last) two values
Reto
  • 1,305
  • 1
  • 18
  • 32
  • brother can you tell how to return a variable which only has input. – Stay Foolish Nov 13 '22 at 08:31
  • void main() { int nata1 = 100; int nata2 = 122; int nata3 = 98; List nata = [nata1, nata2, nata3]; if (nata.length == 1) { print('return one non null value'); } else if (nata.length == 2) { print('return Average of Two'); } else if (nata.length == 3) { print('return Average of greater two score'); } } How to perform the actions that are mentioned in the print ? – Stay Foolish Nov 13 '22 at 10:01
  • If the inputs originate from input fields within a form, only the filled ones will be submitted. How to compute an average in js was answered long, long time ago: https://stackoverflow.com/questions/10359907/how-to-compute-the-sum-and-average-of-elements-in-an-array Same applies on how to sort an array of numbers: https://stackoverflow.com/questions/1063007/how-to-sort-an-array-of-integers-correctly – Reto Nov 13 '22 at 13:17