javascript - 在输出上复制数组的函数

标签 javascript arrays function

我创建了一个包含 2 个函数的脚本,第一个函数计算数组的百分比,而另一个函数计算我的百分比与数组的总和。 但是当我调用我的函数时,我的数组百分比的输出会加倍

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

预期结果

enter image description here

最佳答案

也许是这样的?

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

在你的代码中你做了类似的事情:

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 !

else if(x>50 && < 200)
应该是else if(x>=50 && < 200)

但它没有用,因为之前测试过 0...49 的值,并且在 else if 中不会发生

所以你的代码应该是:

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
}

我的代码是

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 在数组对象上提供了许多方法(在 JS 上一切都是对象!)

mdn 上有关 array.map 方法的文档(带有示例)=> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

mdn 上有关 array.reduce 方法的文档 => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
不要忘记为此使用initialValue。

箭头函数 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
简单示例:

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 
}

关于javascript - 在输出上复制数组的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59690688/

相关文章:

javascript - 使用 javascript 访问 jqgrid 元素

python - Numpy append 不允许串联

java - 将 EnumSet 转换为整数数组

JavaScript - 走十分钟 - 如何正确访问数组元素

javascript - jquery 头文件冲突

javascript - Web API - 无法加载资源 : the server responded with a status of 404

go - Golang 中二进制字节数组的终止指示器?

algorithm - 编写函数将边映射到正整数

javascript - 编写一个函数,返回 4 个数字中最大的一个

Javascript - 分割字符串并在单独的行上输出结果