Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program that reads file information from the console and groups the files according to their extensions
- in the format <file-extension> <[file1, file2, … ]> <total memory in MB>, where total memory is the sum of
- the sizes of the respective files. Extension lines should be sorted by the extension names. The files themselves
- should also be ordered alphabetically. Finally, the information is converted to JSON format and printed.
- The input is passed as array of several strings: each string will contain information about a file in
- the format <name> <extension> <memory>, separated by spaces. Print the file information in JSON format.
- The memory should be printed with 2 places after the decimal point.*/
- "use strict";
- function formatFiles(arr) {
- var cloudMap = {};
- for (var i = 0; i < arr.length; i += 1) {
- arr[i] = arr[i].split(/\s+/g).filter(Boolean);
- var fileName = arr[i][0];
- var extension = arr[i][1];
- var memory = parseFloat(arr[i][2]);
- if (!(extension in cloudMap)) {
- cloudMap[extension] = { 'files': [], 'memory': 0 };
- }
- cloudMap[extension].files.push(fileName);
- cloudMap[extension].memory += memory;
- }
- function sortingObject(object) {
- var sortedKeys = Object.keys(object).sort();
- var sortedObj = {};
- for (var i = 0; i < sortedKeys.length; i += 1) {
- if (object.hasOwnProperty(sortedKeys[i])) {
- sortedObj[sortedKeys[i]] = {};
- sortedObj[sortedKeys[i]].files = object[sortedKeys[i]].files.sort();
- sortedObj[sortedKeys[i]].memory = object[sortedKeys[i]].memory.toFixed(2);
- }
- }
- return sortedObj;
- }
- cloudMap = sortingObject(cloudMap);
- return JSON.stringify(cloudMap);
- }
- console.log(formatFiles([
- 'sentinel .exe 15MB',
- 'zoomIt .msi 3MB',
- 'skype .exe 45MB',
- 'trojanStopper .bat 23MB',
- 'kindleInstaller .exe 120MB',
- 'setup .msi 33.4MB',
- 'winBlock .bat 1MB'
- ]));
- console.log(formatFiles([
- 'eclipse .tar.gz 198.00MB',
- 'uTorrent .gyp 33.02MB',
- 'nodeJS .gyp 14MB',
- 'nakov-naked .jpeg 3MB',
- 'gnuGPL .pdf 5.6MB',
- 'skype .tar.gz 66MB',
- 'selfie .jpeg 7.24MB',
- 'myFiles .tar.gz 783MB'
- ]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement