0

I created a script containing 2 functions, the first calculates the percentage of an array, while the other function calculates the sum of my percentage with my array. But when I call my functions the output where would be my Array Percentage is doubled

const Info_Buyer = {
  Name_Buyer: 'John',
  Spent_Rest: [50, 100, 300, 10, 80],
  Tip: [],
  Total_Spent: 0,
  // The function stores the amount spent for each restaurant and tips depending on the amount spent
  Cal_Tip: function() {
    let n = 0
    while (n < this.Spent_Rest.length) {
      if (this.Spent_Rest[n] < 50) {
        this.Tip.unshift(this.Spent_Rest[n] * 0.2);
      } else if (this.Spent_Rest[n] > 50 && this.Spent_Rest[n] < 200) {
        this.Tip.unshift(this.Spent_Rest[n] * .15);
      } else {
        this.Tip.unshift(this.Spent_Rest[n] * .1);
      }
      n++
    }
    return this.Tip
  },
  // The function sums the value of the tip and the amount spent on the order, showing the total expense as output
  Cal_Total: function() {
    let tip = this.Cal_Tip()
    let n = 0
    while (n < this.Spent_Rest.length) {
      this.Total_Spent += this.Spent_Rest[n] + tip[n]
      n++
    }
    return this.Total_Spent
  }
}
total = Info_Buyer.Cal_Tip()
tip = Info_Buyer.Cal_Total()
console.log(total, tip);

Expected result

enter image description here

2
  • 1
    Cal_Total() calls Cal_Tip(), which adds to the Tip() array. Commented Jan 11, 2020 at 0:44
  • when this.Spent_Rest[n] is 50 your code apply * .1 ( same for this.Spent_Rest[n] is 200 or more Commented Jan 11, 2020 at 0:53

2 Answers 2

1

maybe something like that ?

const Info_Buyer = 
  { Name_Buyer : 'John'
  , Spent_Rest : [50, 100, 300, 10, 80]  
  , Cal_Tip() 
    { // The function stores the amount spent for each restaurant and tips depending on the amount spent
    return this.Spent_Rest.map(x=>
      {
      if (x <= 50)  return x * 0.2
      if (x <= 200) return x * 0.15
      return x * 0.1
      })
    }
  , Cal_Total()
    { // The function sums the value of the tip and the amount spent on the order, showing the total expense as output
    return this.Cal_Tip().reduce((ac,el,n)=>ac +el +this.Spent_Rest[n], 0)
    }
  }

let tip   = Info_Buyer.Cal_Tip()
  , total = Info_Buyer.Cal_Total()

console.log('tip   =', JSON.stringify(tip));
console.log('total =', total );
.as-console-wrapper { max-height: 100% !important; top: 0; }

Expand snippet

in your code you do somethig like that:

function Cal_Tip() {
    let val
    if (x < 50)             val = x * 0.2   // x = 0...49
    else if(x>50 && < 200)  val = x * 0.15  // x = 51...199
    else                    val = x * 0.1   // x = 50 or x = 200...infinite
    return val
}

and you miss x = 50 !

the else if(x>50 && < 200)
should be else if(x>=50 && < 200)

but it is useless because values from 0...49 are tested before and can't happen in this else if

so your code should be:

function Cal_Tip() {
    let val
    if (x <= 50)      val = x * 0.2  
    else if(x <= 200) val = x * 0.15 
    else             val = x * 0.1 
    return val
}

and my code is

function Cal_Tip() {
  if (x <= 50 )  return = x * 0.2    // if (x < 50 ) the function is finished and return x * 0.2
  if (x <= 200)  return = x * 0.15  // if (x < 200) the function is finished and return x * 0.15
  return = x * 0.1                // in aother cases...
}

javascript offers many methods on array object (on JS everythig is object!)

the doc on mdn on the method array.map (with examples) => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

the doc on mdn on the method array.reduce => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
don't forget to use initialValue for this one.

arrows functions https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
quick example:

function functionA(param) { return param *10 }
// become 
functionA = param => param*10  // the arrow make the return too

function functionB(param1,param2) {
  let sum = param1+param2; 
  return sum/2 
}
// become 
functionB =(param1,param2)=>{ // more than 1 arguments -> you should use parenthesis
  let sum = param1+param2;  // + more than 1 line of -> you should use brackets
  return sum/2 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes it is exactly what I expected, but as I am still starting in JS, I got lost in some parts (map and reduce and the arrows functions). In the comment above you had explained that the error was in the conditions but using the same conditions you got the correct result. Why does this happen?
@luismatheus I have added some explanations
0

In fact my solution was simpler than I expected. Instead of this.Cal_Tip () I just called the end result of my function (which would be the Tip array), as @Barmar said the function was called 2 times in Cal_Total. When the Cal_Tip function is called on the out-of-object variable declaration, the Tip value is automatically loaded into the this.Tip declaration. Thanks for the answers.

const Info_Buyer = {
  Name_Buyer: 'John',
  Spent_Rest: [50, 100, 300, 10, 80],
  Tip: [],
  Total_Spent: 0,
  // The function stores the amount spent for each restaurant and tips depending on the amount spent
  Cal_Tip: function() {
    let n = 0
    while (n < this.Spent_Rest.length)
    {
      if (this.Spent_Rest[n] <= 50)
      {
        this.Tip.unshift(this.Spent_Rest[n] * 0.2);
      } 
      else if (this.Spent_Rest[n] <= 200)
      {
        this.Tip.unshift(this.Spent_Rest[n] * .15);
      }
      else 
      {
        this.Tip.unshift(this.Spent_Rest[n] * .1);
      }
      n++
    }
    return this.Tip
  },
  // The function sums the value of the tip and the amount spent on the order, showing the total expense as output
  Cal_Total: function() {
    let tip = this.Tip //<------ MODIFIED AREA
    let n = 0
    while (n < this.Spent_Rest.length) {
      this.Total_Spent += this.Spent_Rest[n] + tip[n]
      n++
    }
    return this.Total_Spent
  }
}
total = Info_Buyer.Cal_Tip()
tip = Info_Buyer.Cal_Total()
console.log(total, tip);

2 Comments

@MisterJojo Thanks for warning, I had not noticed before
I read but thought it would be in relation to the duplication error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.