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');


let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;

process.stdin.on('end', function() {
inputString = inputString.split('\n');


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 = =>  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);

const result = tripleTheChances(chances);

ws.write(result.join('\n') + '\n');


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)
  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];
  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];
  let res = => 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