只需要使用一次功能console.log

时间:2018-08-16 07:11:33

标签: javascript function

我想创建一个小程序,它将计入折扣价。 任务是仅使用// Method to show Progress bar private void showProgressDialogWithTitle(String substring) { progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); //Without this user can hide loader by tapping outside screen progressDialog.setCancelable(false); progressDialog.setMessage(substring); progressDialog.show(); } // Method to hide/ dismiss Progress bar private void hideProgressDialogWithTitle() { progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.dismiss(); } 函数一次。 如果输入价格为负(例如-40),console.log需要显示“无效输入”

我的程序在两个变体中都计入折扣-当价格为正时和价格为负时。

请帮我找到一个错误。预先谢谢你!

这是我的代码:

console log

6 个答案:

答案 0 :(得分:2)

price

可能没有达到您的期望-例如,如果discount为负数,但true为正数,则无论如何它都会求值为every,而事实并非如此如果要检查两者是否都大于零,则可以检查[price, discount]数组中的[price, discount].every(num => num > 0) 项是否大于0。

null

要指示无效的返回值,您可以返回const price = prompt('Please, enter the price:'); const discount = prompt('Please, enter the discount amount:'); function discountPrice(price, discount) { return [price, discount].every(num => num > 0) ? price - price * discount / 100 : null } const newPrice = discountPrice(price, discount); const savedMoney = price - newPrice; const textToPrint = newPrice === null ? 'Invalid Data' : `New price is: ${newPrice} Price without discount: ${price} Discount: ${discount}%' Price with discount:: ${newPrice} Saved: ${savedMoney} `; console.log(textToPrint);。然后,根据该返回值分配要打印的文本字符串。使用条件运算符和模板文字可以减少语法干扰:

  private void AddNewRowToDGV()
        {
            var data = (DataTable) dataGridView1.DataSource;
            var row = data.NewRow();
            row["Cost"] = 1;
            data.Rows.Add(row);    
        }

答案 1 :(得分:0)

const price = prompt('Please, enter the price:');
const discount = prompt('Please, enter the discount amount:');

function discountPrice(price, discount) {
  if (price < 0 || discount <  0) {
    console.log('Invalid data')
    return false
  } else {
    return price - price * discount / 100
    
  }
}

let newPrice = discountPrice(price, discount)
if(newPrice){
let savedMoney = price - newPrice

console.log(
  'New price is:', newPrice, '\n' +
  'Price without discount:', price, '\n' +
  'Discount:', discount, '%', '\n' +
  'Price with discount:', newPrice, '\n' +
  'Saved:', savedMoney);
}

答案 2 :(得分:0)

您可以设置if-else这样的逻辑,使其同时对pricediscount起作用,以检查它们是否为负值。

const price = prompt('Please, enter the price:');
const discount = prompt('Please, enter the discount amount:');

function discountPrice(price, discount) {
  if (price <= 0 || discount <= 0) {
    return 'Invalid data';
  }
  if (price && discount) {
    let newPrice = price - price * discount / 100;
    let savedMoney = price - newPrice;
    return 'New price is: ' + newPrice +', \n' +
    'Price without discount: '+ price +', \n' +
    'Discount: '+ discount +', '%', \n' +
    'Price with discount: '+ newPrice+ ', \n' +
    'Saved: ' + savedMoney;
  }
}

let newPrice = discountPrice(price, discount);
console.log(newPrice);

答案 3 :(得分:0)

  

更正您的DiscountPrice函数

function discountPrice(price, discount) {
  if (price > 0 && discount> 0) {    //         <<--- Here    
    return price - price * discount / 100
  } else {
    console.log('Invalid data')
  }
}

这不是英语。这是JavaScript。 :)

答案 4 :(得分:0)

只需更改以下if条件,

if (newPrice){
        console.log(
          'New price is:', newPrice, '\n' +
          'Price without discount:', price, '\n' +
          'Discount:', discount, '%', '\n' +
          'Price with discount:', newPrice, '\n' +
          'Saved:', savedMoney);
        }

还要在代码中的最终console.log中添加以下if条件

    const price = prompt('Please, enter the price:');
    const discount = prompt('Please, enter the discount amount:');
    
    function discountPrice(price, discount) {
      if ( price > 0 && discount > 0 ) {
        return price - price * discount / 100
      } else {
        console.log('Invalid data')
      }
    }
    
    let newPrice = discountPrice(price, discount)
    let savedMoney = price - newPrice
    if (newPrice){
    console.log(
      'New price is:', newPrice, '\n' +
      'Price without discount:', price, '\n' +
      'Discount:', discount, '%', '\n' +
      'Price with discount:', newPrice, '\n' +
      'Saved:', savedMoney);
    }   

所以修改后的代码将是

{{1}}

希望这会有所帮助。

答案 5 :(得分:0)

问题在于,您始终会将最后一条消息记录到控制台,因此,在condition之前,应先放置console.log进行检查。

您必须检查discountPrice函数的返回值。 如果是数字,则console.log是您的消息,否则不应该。

此外,您还必须将if条件更改为if ((price - discount) > 0)  代替if ((price && discount) > 0)

const price = prompt('Please, enter the price:');
const discount = prompt('Please, enter the discount amount:');

function discountPrice(price, discount) {
  if ((price - discount) > 0) {
    return price - price * discount / 100
  } else {
    console.log('Invalid data')
  }
}

let newPrice = discountPrice(price, discount)
let savedMoney = price - newPrice

if(!isNaN(newPrice)){
console.log(
  'New price is:', newPrice, '\n' +
  'Price without discount:', price, '\n' +
  'Discount:', discount, '%', '\n' +
  'Price with discount:', newPrice, '\n' +
  'Saved:', savedMoney);
}


您也可以将代码更改为这样。

const price = prompt('Please, enter the price:');
const discount = prompt('Please, enter the discount amount:');

if ((price - discount) > 0) {
  let newPrice =  price - price * discount / 100;
  let savedMoney = price - newPrice;
  console.log(
  'New price is:', newPrice, '\n' +
  'Price without discount:', price, '\n' +
  'Discount:', discount, '%', '\n' +
  'Price with discount:', newPrice, '\n' +
  'Saved:', savedMoney);
} else {
  console.log('Invalid data')
}