multiply each item of a array into a new array except one

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();
}

but only one of the 5 tests is returning right.

So you have an array of x numbers, lets say [1,2,3,4,5,6]
ā€¦ and you want to remove a number from the arrayā€¦ and then multiply all numbers by 3ā€¦ is that correct?

If you want a random index removed from the array, this will workā€¦ It also doesnā€™t mutate the input array.

const chances = function(arr){
  let clone = [...arr];
  let rand = Math.floor(Math.random() * clone.length)
  clone.splice(rand,1);
  for (let [i,val] of clone.entries()){ clone[i] = val *3}
return clone;
}

not exactly

the first number in the input of the testā€™s is the length of the array, so I have to make a function that ignores that length but the array itself shouldnā€™t remove any index, just triplicate each value of it and return the result in a new array.

and then there are the tests that are done to see if the function is correct.
So, for example, in the first test the input is 5, 1, 2, 3, 4, 5.

The first number of this input (5) is the length of the array (which in the explanation of the exercise is referred to as ā€˜nā€™ integer) and then the multiplication must happen from the 1, 2, 3, 4, 5 which is the array itself.

In my head I thought that just making the map() method should work but it didnā€™t, it returned a join() error and the explanation of the exercise talks about a ā€œfor loopā€ where i >= 0 and i < n and I didnā€™t understand.

No worries, hereā€™s a couple of ways to make that happen.

with a new Array and then fill it from arr[1] ā†’ clone[0]

const chances = function(arr){
  let clone = new Array(arr[0]);
  for (var i = 1; i < arr.length; i++ ){ 
    clone[i-1] = arr[i] *3}
return clone;
}

or the same but push() (doesnā€™t evaluate [i-1] every loop)

  const chances = function(arr){
      let clone = [];
      for (var i = 1; i < arr.length; i++ ){ 
        clone.push(arr[i] *3)
    return clone;
    }

or clone then shift() then iterateā€¦

const chances = function(arr){
  let clone = [...arr];
  clone.shift();
  for(let [i,val] of clone.entries()){clone[i] = val * 3}
return clone;
}

or clone shift() then map() (your creating another copy)

const chances = function(arr){
  let clone = [...arr];
  clone.shift();
  let res = clone.map(x => x * 3)
return res
}

Unfortunately you cant do thisā€¦ :slightly_smiling_face: (it would be super nice)

const chances = arr => [...arr].shift().map(x => x * 3)

1 Like

My bad, shift() returns the removed chunk not the array.
This one liner is possible:

const chances = ([...x]) => x.splice(1,x.length).map(x => x * 3);

as a bonus it doesnā€™t mutate the original array but you have to use parenthesis ( ) =>ā€¦

1 Like