Cash register 2/7 works but error appear


#1


#2

Hey @tonyhuang19! For the first one you added 0.99 cents its suppose to be 0.98 cents. #.01CentsMakesAllTheDiffrence


#3

@amanuel2 So I decided to add variables because of the different types of cost. Then i called the methods with the variables as my parameters and it worked. My question is, wouldn't this be more efficient than simply calling numbers?

var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost;
}
};

//call the add method for our items
var eggs = .98;
var milk = 1.23;
var magazine = 4.99;
var chocolate = .45;

cashRegister.add(eggs + milk + magazine + chocolate);

//Show the total bill
console.log('Your bill is '+cashRegister.total);


#4

You could do user input. So instead of this:

Do this? maybe if thats what you are talking about:

var eggs = parseInt(prompt("How many eggs?"));
var milk =  parseInt(prompt("How many milk?"));
var magazine =  parseInt(prompt("How many magazine?"));
var chocolate =  parseInt(prompt("How many chocolate?"));

#5

Not quite,** parseInt** seems to look like a new code to me. What does that do? Also, Prompting the user in your code would only ask them to enter a value. But I'm guessing we could always enter the cashRegister.add code to add their entered results.


#6

Yea, what is it that you exactly want? parseInt basically converts a string to an integer. Ok so let Mdn explain this clearly:

Description

The parseInt function converts its first argument to a string, parses it, and returns an integer or NaN. If not NaN, the returned value will be the decimal integer representation of the first argument taken as a number in the specified radix (base). For example, a radix of 10 indicates to convert from a decimal number, 8 octal, 16 hexadecimal, and so on. For radices above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

If radix is undefined or 0 (or absent), JavaScript assumes the following:

If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
If the input string begins with any other value, the radix is 10 (decimal).
If the first character cannot be converted to a number, parseInt returns NaN.

For arithmetic purposes, the NaN value is not a number in any radix. You can call the isNaN function to determine if the result of parseInt is NaN. If NaN is passed on to arithmetic operations, the operation results will also be NaN.

To convert number to its string literal in a particular radix use intValue.toString(radix).