Created
April 2, 2016 13:57
-
-
Save BucleSinFin/ad7bab88baa44b082d5a4537e3cdee03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Poiščite node.js modul na npmjs.com, ga namestite v okolju runnable.com in ga | |
uporabite. Naredite funkcijo, ki iz datoteke prebere števila in jih sešteje med seboj.*/ | |
var fs = require('fs'); | |
var numbers = '0, 1, 3.9, -5, 100 \n200, 300 \n-50'; | |
fs.writeFileSync('file', numbers); | |
function sum(file_name) { | |
var x = fs.readFileSync(file_name, 'utf-8').split(/[\n,]+/); | |
return x.reduce(function(a, b){return parseFloat(a) + parseFloat(b)}); | |
} | |
console.log('Vsota števil v datoteki: ' + sum('file')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Zelo dobra rešitev. Ker vidim da že uporabljaš reduce funkcijo nad arrayem in split stringa, tale modul je precej uporaben in naredi pisanje kode v javascriptu, precej bolj uporabno https://lodash.com/, tu najdeš dokumentacijo https://lodash.com/docs. Lodash je kot swiss army knife za javascript.