Reputation: 59
I just need to remove the decimals from the data variable using the following function. Can't seem to manage it at the moment.
maths.multiplyInteger = function(sequence) {
if (sequence.length == 0) {
return 0;
}
var result = maths.getInteger(sequence[0]);
function loop(data) {
for (var i = 1, length = sequence.length; i < length; i++) {
var result = result * maths.getInteger(sequence[i]);
}
}
loop(sequence);
return {
input: sequence,
output: result
}
};
Data variable looks as follows:
var data = {
first: [3.57, 2.43, '043'],
second: [7.26, 1.43, '025'],
third: ['076', 3.0, 6.42],
};
Upvotes: 3
Views: 82
Reputation: 41893
Math.floor()
works fine but firstly you have to refer to the elements properly. Suggested approach: Object.keys()
and Array#map
.
var data = {
first: [3.57, 2.43, '043'],
second: [7.26, 1.43, '025'],
third: ['076', 3.0, 6.42],
},
res = {};
Object.keys(data).forEach(function(v){
res[v] = data[v].map(c => Math.floor(c)).reduce((a,b) => a * b);
})
console.log(res);
//Object.keys() function returns array of all keys of the data object, it will look
//like: ['first', 'second', 'third']
//Then, use map function to iterate over it to catch every key value,
//which is an array (for example - [3.57, 2.43, '043']
//Then, catch every element in this array with map function and round it down
//using Math.floor() function (to the nearest integer under)
Upvotes: 4
Reputation: 386550
You could map the result of Math.floor
.
var data = { first: [3.57, 2.43, '043'], second: [7.26, 1.43, '025'], third: ['076', 3.0, 6.42] },
result = Object.keys(data).map(function (k) { return data[k].map(Math.floor); });
console.log(result);
Upvotes: 1