typeof给了我一个数字,但是同一个变量的console.log给了我NAN我该如何解决这个问题

时间:2019-11-18 18:27:38

标签: javascript

store = {
  bags: {
    inventory: 30,
    cost: 12.99
  },
  pen: {
    inventory: 30,
    cost: 34.23
  }
};
const buy = (x, y) => {
  return new Promise((resolve, reject) => {
    if (x === "bags" && y >= 1 && y < 30) {
      resolve(parseInt(y * store.bags.cost));
    } else {
      reject("out of order");
    }
  });
};

const buy1 = (x, y) => {
  return new Promise((resolve, reject) => {
    if (x === "pen" && y >= 1 && y < 30) {
      resolve(parseInt(y * store.pen.cost));
    } else {
      reject("out of order");
    }
  });
};

const totalBag = [];
const totalPen = [];

parseInt(totalBag.push(buy("bags", 12)));
parseInt(totalPen.push(buy1("pen", 12)));

const total = () => {
  return new Promise((resolve, reject) => {
    if (x === "pen" && y >= 1 && y < 30) {
      resolve(parseInt(y * store.pen.cost));
    } else {
      reject("out of order");
    }
  });
};

totalsum = 0;
totalsum = parseInt(totalBag) + parseInt(totalPen);

console.log(typeof totalsum);
console.log(totalsum);

2 个答案:

答案 0 :(得分:0)

这是因为typeof NaN = number,奇怪但真实

答案 1 :(得分:0)

**

非常感谢您的所有评论,我终于明白了

** 商店= {

bags : { 
    inventory : 30,

    cost : 12.99

},

pen : {

   inventory : 30,

   cost : 34.23
}

};

const totalBag = [];

const totalPen = [];

const buy =(x,y,)=> {

return new Promise((resolve, reject)=>{

    if(x ==='bags'  && y >= 1 && y< 30 ){

        resolve(totalBag.push(parseInt((y*store.bags.cost))));

    }else { reject ('out of order');}; 

});

}; const buy1 =(x1,y1)=> {

return new Promise((resolve, reject)=>{

    if(x1 ==='pen'  && y1 >= 1 && y1< 30 ){

        resolve(totalPen.push((parseInt((y1*store.pen.cost)))));

    }else { reject ('out of order');}; 

}); 

};

const amtBag = buy('bags',10);

const amtPen = buy1('pen',10);

让tbag = Number(totalBag);

让tpen = Number(totalPen);

让TotalPurchase = console.log(tbag + tpen);