2

here what need to be done Implement a function addWithDelay() which will work with parameters according to the conditions below:

  • If all parameters are positive numbers, it should return within 1000ms timeframe a Promise containing a sum of all the numbers provided as parameters .
  • If any parameter is represented by a negative number, function should return an error message: “Error! Some parameter is a negative number!" using “reject()” option. Here my code
function addWithDelay(
    param1 = 633,
    param2 = 345,
    param3 = 986,
    param4 = 234,
    param5 = 532,
    ){
        values = [param1, param2, param3, param4, param5];
        var result = values.filter(function(number) {
            return number < 0;
          });
        if (number > 0){ 
            return Promise.resolve((param1+param2+param3+param4+param5),1000)
            }   else {
                return Promise.reject('Error! Some parameter is a negative number!')
            }
    }
    console.log(addWithDelay())

for some reason it always returned an error message

srk
  • 1,625
  • 1
  • 10
  • 26

3 Answers3

2
  1. You can use some instead of filter to look for a negative number in the array.
  2. You can use setTimeout inside the returned promise.
  3. use reduce to sum the array values.

I've attached a snippet with the changes.

function addWithDelay(
    param1 = 633,
    param2 = 345,
    param3 = 986,
    param4 = 234,
    param5 = 532,
    )
 {
     const values = [param1, param2, param3, param4, param5];
     const isNegativeExist = values.some(x => x < 0);
     if (!isNegativeExist) { 
       const sum = values.reduce((a, b) => a + b, 0);
       return new Promise((resolve) => setTimeout(() => resolve(sum), 1000));
     }
     else {
         return Promise.reject('Error! Some parameter is a negative number!')
     }
 }
    
addWithDelay().then(res => console.log(res));
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
  • 1
    This works fine with negative values, but it didnt returne a sum of positive values – Stanislav Step Apr 14 '21 at 12:59
  • Oh, i missed the sum part, fixed it now, please check again (: – Ran Turner Apr 14 '21 at 13:02
  • ```function addWithDelay( param1 = 633, param2 = 345, param3 = 986, param4 = 234, param5 = 532, ) { values = [param1, param2, param3, param4, param5]; const isNegativeExist = values.some(x => x < 0); if (!isNegativeExist) { return Promise.resolve((param1+param2+param3+param4+param5),1000) } else { return Promise.reject('Error! Some parameter is a negative number!') } } console.log(addWithDelay())``` – Stanislav Step Apr 14 '21 at 13:03
  • I would appreciate if you upvote the answer (: – Ran Turner Apr 14 '21 at 13:33
  • Sure!) just found out how) – Stanislav Step Apr 14 '21 at 13:40
1

You are trying to access number out of its scope. Instead, try checking result.length. Also, I have fixed your Promise. You don't need Promise.resolve; you just need to use resolve from the Promise function's arguments. The link Felix provided in the comments is very useful and so is MDN's documentation.

function addWithDelay(
  param1 = 633,
  param2 = 345,
  param3 = -986,
  param4 = 234,
  param5 = 532,
) {
  return new Promise(function(resolve, reject) {
    setTimeout(() => {
      values = [param1, param2, param3, param4, param5];
      var result = values.filter(function(number) {
        return number < 0;
      });
      if (!result.length) {
        resolve(param1 + param2 + param3 + param4 + param5)
      } else {
        reject('Error! Some parameter is a negative number!')
      }
    }, 1000);
  });
}
(async () => console.log(await addWithDelay()))()
Rojo
  • 2,749
  • 1
  • 13
  • 34
1

You are checking number instead of result.length in line 12 to infer if there is a negative number and it is necessary to implement a setTimeout() function to returno the correct value you are expecting. After the function call should have a then statement to print the results.

try this

function addWithDelay(
    param1 = 633,
    param2 = 345,
    param3 = 986,
    param4 = 234,
    param5 = 532,
) {
    values = [param1, param2, param3, param4, param5];
    var result = values.filter(function (number) {
        return number < 0;
    });
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (result.length === 0) {
                resolve(values.reduce((i, j) => i+j))
            }
            else {
                reject('Error! Some parameter is a negative number!')
            }
        }, 1000);
    });
}
addWithDelay()
    .then(d => console.log(d))
    .catch(e => console.error(e));
Joao Pedro P.P
  • 125
  • 2
  • 7