Advertisement
dimipan80

Exams - Odd or Even Counter (on JavaScript)

Dec 30th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Pesho is given N sets of numbers, and he has to count the odd numbers in each set, and then compare them.
  2.  The number C shows how many numbers are there in a set. Then you are given a string S holding one of the words
  3.  "odd" or "even" showing you what numbers should be counted.  Then you are given N * C numbers representing all sets.
  4.  Your task is to count the odd or even numbers in each set, and then say in which set has most S numbers.
  5.  The set with most S numbers should be represented as ordinal number word e.g. "First", "Second", "Third",
  6.  "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth".
  7.  If the count of one or more sets is equal, print the first one with biggest count.
  8.  If there is no set holding odd or even numbers print "No".
  9.  The input consists of three input values, each at separate line:
  10.  The first line holds an integer N – the count of sets.
  11.  The second line hold an integer C – the count of numbers in each set.
  12.  The third line holds a string S  holding either "odd" or "even" showing what numbers to count.
  13.  The output data should be printed on the console. It consists of exactly 1 line.
  14.  Print on the console the following formatted string "{0} set has the most {1} numbers: {2}",
  15.  where {0} is the set as ordinal string {1} is the value of S and {2} is the biggest count of S numbers.
  16.  If there is no set holding odd or even numbers print "No". */
  17.  
  18. "use strict";
  19.  
  20. function solve(args) {
  21.     var setLength = parseInt(args[1]);
  22.     var targetType = args[2];
  23.    
  24.     var i;
  25.     var setCounter = 0;
  26.     var numsCounter = 0;
  27.     var maxTypeCount = 0;
  28.     var setMaxType = 0;
  29.     var typeCounter = 0;
  30.     for (i = 3; i < args.length; i += 1) {
  31.         var number = parseInt(args[i]);
  32.         var isOdd = number % 2 != 0;
  33.         if ((isOdd && targetType == 'odd') || (!isOdd && targetType == 'even')) {
  34.             typeCounter += 1;
  35.         }
  36.         numsCounter += 1;
  37.         if (numsCounter == setLength) {
  38.             setCounter += 1;
  39.             if (typeCounter > maxTypeCount) {
  40.                 maxTypeCount = typeCounter;
  41.                 setMaxType = setCounter;
  42.                 if (maxTypeCount == setLength) break;
  43.             }
  44.             typeCounter = 0;
  45.             numsCounter = 0;
  46.         }
  47.     }
  48.    
  49.     var setNames = [0, 'First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth', 'Seventh', 'Eighth', 'Ninth', 'Tenth'];
  50.    
  51.     if (!maxTypeCount) {
  52.         console.log('No');
  53.     } else {
  54.         console.log("%s set has the most %s numbers: %d", setNames[setMaxType], targetType, maxTypeCount);
  55.     }
  56. }
  57.  
  58. solve(['2', '5', 'odd', '6', '4', '12', '8', '199', '15', '21', '7', '3', '5']);
  59. solve(['3', '2', 'even', '1', '3', '5', '7', '9', '11']);
  60. solve(['3', '2', 'odd', '1', '3', '5', '9', '151', '193']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement