The function returns invalid values

andrea2010

Implement the getarrayproduct function, which gets an array of numbers and returns an array of the same size, where numbers[I] is equal to the product of all elements of the array to the right and left of that element.

Notes:

The initial array contains at least 2 elements The array contains only positive numbers The numbers can be repeated Examples:

getArrayProduct([1,5,2]) === [10,2,5]

The first element 10 is the product of all array's elements except the first element 1 The second element 2 is the product of all array's elements except the second element 5 The third element 5 is the product of all array's elements except the third element 2

and

 getArrayProduct([12,20]) === [20,12]

The first element in array 20 is the product of all array's elements except the first element The second element 12 is the product of all array's elements except the second element

My code

function getArrayProduct(numbers) {

  let arr = []
  let a = 0;
  let l = numbers.length;

  if (numbers.length == 2) {
    arr = numbers.reverse()
    return arr
  }

  while(true) {
    if (a + 1 !== NaN) {
      if ((a + 1) == numbers.length) {
        arr[a] = numbers[a] * numbers[0]
      } else {
        arr[a] = numbers[a] * numbers[a+1]
      }
    }

    if (a > numbers.length - 2) {
      break;
    }

    a++;
    l--;
  }

  return arr
}

Function 'getArrayProduct' should return correct array for:

[4, 5, 2, 19, 8, 80]

Expected:

"[121600,97280,243200,25600,60800,6080]"

Received:

"[20,10,38,152,640,320]"

and getArrayProduct([1,5,2]) === [10,2,5] - (expected) and my result [5,10,2]

manaschopra98

Alternate approach: Eg: [1,5,2] Multiply all the numbers in array at once. you will get ans 1x5x2 = 10

now your output array will be [product/1, product/5, product/2] == [10/1,10/5,10/2] == [10,2,5]

This method will remove the effort of multiplying same numbers again and again, for each index in array.

function getArrayProduct(numbers) {

  let arr = []
  let product = 1, i=0;

for (i=0;i<numbers.length;i++){
    product=product*numbers[i];
}

for(i=0;i<numbers.length;i++)
{ 
    arr[i] = product/numbers[i];
}

return arr;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Function drops invalid values in dataframes but then it returns original dataframes with invalid values

OpenProcess function returns invalid handles

Powershell function returns an invalid object

get function returns invalid value

mongodb isValid function returns true for invalid ObjectIDs

Function returns invalid literal for int() with base 10: ''

Powershell function returns intermediate values

Print function returns two values

checkPrime function returns incorrect values

counter function returns wrong values

Function which returns multiple values

Firebase / Google Cloud Function cron function returns INVALID_ARGUMENT

Pandas: checking invalid values in to_datetime function

Function in matlab guide to check for invalid values

A function that returns different column values based on input

Graphite Returns All values, rather than function

Oracle: Create a function that returns a set of values?

Same function with same inputs returns different values

Order function returns different values as vector

Elixir doctest fails for function that returns random values

PL/SQL Function returns to many values

Function returns all values as null, even though they are not

return function returns array values using Closure

How to document a function that returns specific values?

Returns multiple values from a PHP function

JavaScript constructor function returns values as undefined

Function returns json instead of only values

Summarize with a function that returns multiple values in a list

Programming a function that saves and returns values in python