OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

JavaScript - number in method is showing typeof string after I have performed a mathematical operation on it

  • Thread starter Thread starter apex2022
  • Start date Start date
A

apex2022

Guest
I'm trying to figure out why something that should be a number is showing typeof equals string. Here is my code:


Code:
const goToDinner = () => {
  const menu = [
    {
      dishName: "Beef Tenderloin",
      price: 14.75,
    },
    {
      dishName: "Ribeye Steak",
      price: 17.50,
    },
    {
      dishName: "Barbecue Chicken",
      price: 13.25,
    },
  ];
  let menuList = "";
  for (let i = 0; i < menu.length; i++) {
    menuList += `${i + 1}. ${menu[i].dishName}: $${menu[i].price}\n`;
  }
  const dinnerChoice = parseInt(prompt(
    `Please select your choice of meal:\n${menuList}`,
  ));
  if (
    !Number.isNaN(dinnerChoice) && dinnerChoice > 0 &&
    dinnerChoice <= menu.length
  ) {
    console.log(`You have chosen the ${menu[dinnerChoice - 1].dishName}.`);
  }
  const preTipTotal = parseFloat(menu[dinnerChoice - 1].price).toFixed(2);
  console.log(`The preTip amount = ${preTipTotal}.`);
  const tipPercentage = 0.095;
  const tipCost = (preTip, tipPercent) => {
    const tipResult = (preTip * tipPercent).toFixed(2);
    console.log(`The tip comes to ${tipResult}`);
    console.log(`typeof tipResult = ${typeof tipResult}.`);
    return tipResult;
  };
  console.log(`Is preTipTotal a number? ${typeof preTipTotal}`);
  const totalBill = preTipTotal + tipCost(preTipTotal, tipPercentage);
  console.log(`Your total bill is $${totalBill}.`);
};

goToDinner();

When I choose an option, say #2 - the Ribeye Steak for 17.50, the tip is 1.66. But on the last line of the function, the total shown is $17.501.66 instead of $19.16. I traced the problem down to the tipCost inner function. typeof tipResult shows it is a string. But the arguments fed into tipCost's parameters - preTipTotal and tipPercentage - are both numbers. Moreover, I perform multiplication on them and use a Number method (toFixed) for the result. So why is tipResult coming up as a string instead of a number?

I know I could probably correct the issue with a parseFloat on the call to tipCost(), but that's not enough. I need to be able to explain why tipResult is coming up as a string instead of a number, and I can't do that because it makes no sense to me.

<p>I'm trying to figure out why something that should be a number is showing typeof equals string. Here is my code:</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>const goToDinner = () => {
const menu = [
{
dishName: "Beef Tenderloin",
price: 14.75,
},
{
dishName: "Ribeye Steak",
price: 17.50,
},
{
dishName: "Barbecue Chicken",
price: 13.25,
},
];
let menuList = "";
for (let i = 0; i < menu.length; i++) {
menuList += `${i + 1}. ${menu.dishName}: $${menu.price}\n`;
}
const dinnerChoice = parseInt(prompt(
`Please select your choice of meal:\n${menuList}`,
));
if (
!Number.isNaN(dinnerChoice) && dinnerChoice > 0 &&
dinnerChoice <= menu.length
) {
console.log(`You have chosen the ${menu[dinnerChoice - 1].dishName}.`);
}
const preTipTotal = parseFloat(menu[dinnerChoice - 1].price).toFixed(2);
console.log(`The preTip amount = ${preTipTotal}.`);
const tipPercentage = 0.095;
const tipCost = (preTip, tipPercent) => {
const tipResult = (preTip * tipPercent).toFixed(2);
console.log(`The tip comes to ${tipResult}`);
console.log(`typeof tipResult = ${typeof tipResult}.`);
return tipResult;
};
console.log(`Is preTipTotal a number? ${typeof preTipTotal}`);
const totalBill = preTipTotal + tipCost(preTipTotal, tipPercentage);
console.log(`Your total bill is $${totalBill}.`);
};

goToDinner();</code></pre>
</div>
</div>
</p>
<p>When I choose an option, say #2 - the Ribeye Steak for 17.50, the tip is 1.66. But on the last line of the function, the total shown is $17.501.66 instead of $19.16. I traced the problem down to the tipCost inner function. typeof tipResult shows it is a string. But the arguments fed into tipCost's parameters - preTipTotal and tipPercentage - are both numbers. Moreover, I perform multiplication on them and use a Number method (toFixed) for the result. So why is tipResult coming up as a string instead of a number?</p>
<p>I know I could probably correct the issue with a parseFloat on the call to tipCost(), but that's not enough. I need to be able to explain why tipResult is coming up as a string instead of a number, and I can't do that because it makes no sense to me.</p>
 

Latest posts

U
Replies
0
Views
1
user3658366
U
G
Replies
0
Views
1
Giampaolo Levorato
G
M
Replies
0
Views
1
Marcelo Rodrigo Nascimento
M
Top