Working With Numbers Flashcards
What is a floating point number?
3.14
What is an example of a number using scientific notation?
9e-6
What is an example of an integer?
127 and -112345
What stores the number 225,622 in the variablemilesToTheMoon?
var milesToTheMoon = 225622;
Given the following code:
var userAge = ‘32’;
What type of value is stored in the variable userAge?
string
- Create a variable named age and store your age in the variable.
- Add a second variable named price and store a floating point value (a number with a decimal) in it.
var age = 26; var price = 0.99;
What adds 20 to the current value of the variable points, then stores the result back into points.
points += 20
- Create a new variable salesTotal that contains the total of the retailPrice multiplied by thequantity variable.
var wholesalePrice = 5.45;
var retailPrice = 9.99;
var quantity = 47;
- Create another variable named profit. It should hold the value of the salesTotal variable minus the wholesalePrice multiplied by the quantity. In other words, if you sold 47 items for 9.99 but only paid 5.45 for each item, how much money did you make?
- Create one last variable name profitPerUnit. In that variable store the amount of profit you made for each unit. You can calculate this by dividing the profit by the quantity.
var salesTotal = retailPrice * quantity;
var profit = salesTotal - wholesalePrice * quantity;
var profitPerUnit = profit / quantity
Let’s assume we have 10
tags on a page. Each is 190 pixels wide. Using the two variables in this script, create a new variable named totalWidth that multiples the width by the numOfDivsvariable. You’ll need to use a JavaScript function to retrieve the number value from the string in the width variable.
var width = '190px'; var numOfDivs = 10;
var totalWidth = parseInt(width) * numOfDivs
What will appear in the JavaScript console after this code runs?
console.log( parseInt( ‘.5 FTE’ ));
NaN
- Because the first character is a . (decimal point) the parseInt() method sees this as a string, not a number.
What will appear in the JavaScript console after this code runs?
console.log( Math.round( 3.9 ) );
4
What will appear in the JavaScript console after this code runs?
console.log( parseInt( ‘7 days a week’ ) );
7
What JavaScript method takes a string and tries to convert it to an integer (a whole number)?
parseInt()
- For this Code Challenge you need to use JavaScript’s Math methods. Open an alert dialog and display the temperature variable rounded to the nearest integer.
var temperature = 37.5
- Open an alert dialog a second time and display the temperature variable rounded downward to the nearest integer (hint: down is toward the “floor”.)
alert(Math.round(temperature));
alert(Math.floor(temperature));
What code produces a random number from 1 to 6 and stores it inside a variable named dieRoll?
var dieRoll = Math.floor( Math.random() * 6 ) + 1;