Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by VKNikov on 26.7.2014 г..
- */
- //Problem 16. Cards Frequencies
- //Write a JavaScript function findCardFrequency(value) that that accepts the following parameters: array of several
- // cards (face + suit), separated by a space. The function calculates and prints at the console the frequency of
- // each card face in format "card_face -> frequency". The frequency is calculated by the formula appearances / N and is
- // expressed in percentages with exactly 2 digits after the decimal point. The card faces with their frequency
- // should be printed in the order of the card face's first appearance in the input. The same card can appear multiple
- // times in the input, but its face should be listed only once in the output. Write JS program cardFrequencies.js that
- // invokes your function with the sample input data below and prints the output at the console.
- console.log('This script takes an array of several cards and prints the frequency of each card face.');
- console.log();
- function findCardFrequency(arr) {
- "use strict";
- var splittedInput = arr.split(/ /g);
- var n = splittedInput.length;
- var container = {};
- //This loop checks whether a card is present in the container and if not puts it there as key, the value stores
- // the number of times the card is present in the input.
- for (var i = 0; i < n; i++) {
- var character = splittedInput[i].charAt(0);
- if (!(character in container)) {
- container[character] = 1;
- } else {
- container[character] += 1;
- }
- }
- //This loop calculates the ratio of every single card in the input and prints it on the console in percents. Note
- //that it does not print the cards from the container but from the input array.
- for (var i = 0; i < n; i++) {
- var character = splittedInput[i].charAt(0);
- if ((character in container)) {
- if (character == 1) {
- console.log(character + '0 -> ' + (container[character]/n * 100).toFixed(2) + '%');
- } else {
- console.log(character + ' -> ' + (container[character]/n * 100).toFixed(2) + '%');
- }
- delete container[character];
- }
- }
- }
- findCardFrequency('8♥ 2♣ 4♦ 10♦ J♥ A♠ K♦ 10♥ K♠ K♦');
- findCardFrequency('J♥ 2♣ 2♦ 2♥ 2♦ 2♠ 2♦ J♥ 2♠');
- findCardFrequency('10♣ 10♥');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement