I’m studying javascript but I get stuck in a exercise for like 3 days.
In tests, the first line of the input files contains an integer n, which denotes the number of elements in a array odds. each row i of the subsequent n rows (where 0 <= i < n), contains an integer describing odds[i] where i >= 0 and i < n.
test examples:
input: 5 2 3 5 8 10
output: 6 9 15 24 30
where: odds = [2, 3, 5, 8, 10]
its length is 5
and the result is the multiplication of each item ignoring the length number.
the code:
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
1. Complete the 'tripleTheChances' function below.
2. The function's return is a variable of type INTEGER_ARRAY.
3. The function accepts the odds parameter of type INTEGER_ARRAY.
*/
function tripleTheChances(chances) {
// enter your code here
const multiplyByThree = chances.map((item) => item* 3)
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const chancesCount = parseInt(readLine().trim(), 10);
let chances = [];
for (let i = 0; i < chancesCount; i++) {
const chancesItem = parseInt(readLine().trim(), 10);
chances.push(chancesItem);
}
const result = tripleTheChances(chances);
ws.write(result.join('\n') + '\n');
ws.end();
}
I try many different ways, but only one of the 5 tests is returning right. I think that at the end I have use a loop for this instead of a array.map() but I can figure out how.