How do you create a function in Javascript which, given a number, returns how many hundreds fit into that number - and then rounds to 2 decimal places?
The code I have:
function howManyHundreds(num) {
return (num / 100.00);
}
However, it just returns solid numbers. For ex. 55/100 = 0, instead of .55.
The assignment suggests using the modulo operator (%) to help, but I am unsure how to use that for rounding to 2 decimal places, not just the remainder.
Calling your function here displays .55 in alert box:
<!DOCTYPE html>
<html>
<head>
<script>
function howManyHundreds(num) {
return (num / 100.00);
}
</script>
</head>
<body>
<script>
alert(howManyHundreds(55));
</script>
</body>
</html>
If you want to use the mod operator you could do something like this:
(100 % .55 = .45)
<!DOCTYPE html>
<html>
<head>
<script>
function howManyHundreds(num) {
var mod = 100.00 % num
return mod.toFixed(2)
}
</script>
</head>
<body>
<script>
alert(howManyHundreds(.55));
</script>
</body>
</html>
The function you have looks valid and returns the value you are expecting.
How ever this is how I tested opening Chrome Console and define below steps and you can verify the output.
step 1 : caller = {};
step 2 (in case you want to fix the output by 2 decimals : caller.execute = function howManyHundreds(num) {
return Number((Number(num) / 100.00).toFixed(2));
}
step 2 (in case you want to round the output) : caller.execute = function howManyHundreds(num) {
return Math.round((Number(num) / 100.00).toFixed(2));
}
step 3 : caller.execute(3444); // Test your output.
Hope this helps.
Related
Type casting array elements form string to Number using array.map(Number).
I need the numbers to have 2 decimal points. Problem I am having is array.map(Number) drops zeros.
example 1: converts the string “10.50” to 10.5
example 2: converts the string “29.10” to 29.1
Here is my script:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>
</title>
</head>
<body>
<script type="text/javascript">
var bills = [50.23, 19.12, 34.01,
100.11, 12.15, 9.90, 29.11, 12.99,
10.00, 99.22, 102.20, 100.10, 6.77, 2.22];
console.log(bills); // these are logged as numbers
var billsTips = bills.map(function(num) {
var num = num + num * .15;
return num.toFixed(2);
});
console.log(billsTips); // these are logged as strings
var totals=billsTips.map(Number);
console.log(totals); // these are logged as numbers with zeros dropped
console.log (typeof(totals[8])); // no longer a string, is as type number
console.log(totals[8]); // I would like 11.5 to be 11.50
</script>
</body>
</html>
Let's say I have <div id="mydiv>>111</div> in html
and I want to get this number by using GetElementbyID(myid).value and convert it to integer
I have tried ParseInt, ParseInt(x, 10), Number(x)....
They all returned NaN. Why?
Note: It works if the number is in a text field, but I want to take it from a div in body.
Change :
document.getElementById("mydiv").value;
To :
document.getElementById("mydiv").innerHTML;
Example :
<html>
<head>
<style>
</style>
</head>
<body>
<div id="mydiv">111</div>
<script>
var txt = document.getElementById("mydiv").innerHTML;
var num = parseInt(txt);
alert(num);
</script>
</body>
</html>
I am trying to get a result from this function. However, nothing is being returned onto my page, just "This example calls a function which performs a calculation, and returns the result:". I need this function to get every postcode in the database from the properties tables and find the distance. Also, one postcode is set always going to be the same so I need to set that and the other postcode is going to be a postcode from the properties table.
<?php
include ("../connect.php");
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>This example calls a function which performs a calculation, and returns the result:</p>
<p id="demo"></p>
<script type="text/javascript">
var x = getDistance(52.482799000000000, -2.000643000000000, 52.48463500000000, -1.980759000000000);
function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {
$earth_radius = 6371;
$postLat = deg2rad($latitude2 - $latitude1);
$postLon = deg2rad($longitude2 - $longitude1);
$a = sin($postLat/2) * sin($postLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($postLon/2) * sin($postLon/2);
$c = 2 * asin(sqrt($a));
$d = $earth_radius * $c;
//$distance_miles = $km * 0.621371; //distance in miles
return $d;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
Where are you using the x variable?
I would expect something like this:
<p>This example calls a function which performs a calculation, and returns the result:
<div id="result">
</div>
</p>
var x = getDistance(52.482799000000000, -2.000643000000000, 52.48463500000000, -1.980759000000000);
$('#result').text(x);
I want to write a script that multiplies any number in a text field with itself by the push of a button and gives the result as an alert.
I'm completely new to Javascript (and have to write my first exam later today).
The syntax is killing me, sometimes so similar to Java, but than again not.
Here's what I came up with so far:
<!DOCTYPE html>
<html>
<head>
<script>
function myMultiply()
{
var x= $('#num1').val();
var y= x*x;
alert(x+" times "+x+" equals "+y);
return false;
}
</script>
</head>
<body>
<input type="text" id="num1">
<button onclick="myMultiply()">Try it</button>
<p>By clicking the button above, the value in the text field will be multiplied with itself.</p>
</body>
</html>
You'll want to make sure you parse the input value as it will be a string when you query for it. To operate on it using multiplication, you need a number. You'll usually want to pass 10 as the second radix parameter as there are different implementations of parseInt
function myMultiply() {
var x = parseInt($('#num1').val(), 10);
var y = x*x;
alert(x + " times " + x + " equals " + y);
return false;
}
You cant multiply string it will be concatenated, parse value to int using parseInt first
parseInt
function myMultiply()
{
var x= parseInt($('#num1').val(), 10);
var y= x*x;
alert(x+" times "+x+" equals "+y);
return false;
}
try replacing var y=x*x; with var y=Number(x)*Number(x);
Along with other answers indicating you should parseInt it should be noted that you aren't currently including jQuery (which gives you access to the $(".element") notation).
jQuery is a very common javascript library that saves a lot of time for very common Javascript tasks (selectors, events etc). You'll see the $() notation in many tutorials and to use it you need to include jQuery.
This will work:
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
function myMultiply()
{
var x= parseInt( $('#num1').val(), 10 );
var y= x*x;
alert(x+" times "+x+" equals "+y);
return false;
}
</script>
</head>
<body>
<input type="text" id="num1" />
<button onclick="myMultiply()">Try it</button>
<p>By clicking the button above, the value in the text field will be multiplied with itself.</p>
</body>
</html>
Your code is fine. You are simply missing the jquery include.
Add <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> right above your other script and everything works unchanged.
Javascript will parse strings and convert them to numbers automatically when it sees that you are trying to multiply. "4" * "2" is 8, not "44" or "42" or any other magical combination. You have a syntax error by referring to $ without actually including jQuery as a required script, so the function ends up being undefined.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function myMultiply()
{
var x= $('#num1').val();
var y= x*x;
alert(x+" times "+x+" equals "+y);
return false;
}
</script>
</head>
<body>
<input type="text" id="num1">
<button onclick="myMultiply()">Try it</button>
<p>By clicking the button above, the value in the text field will be multiplied with itself.</p>
</body>
</html>
I have a piece of code that counts from 0 to a specified number with a specified delay.
The problem is that it adds by 1 and I want it to add by 0.01
How to do it? the code is as follows:
<!DOCTYPE HTML>
<html>
<head>
<style>body{font:11px verdana;color:#555;}</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
var max = 20;// set with php
$(incCounter);
function incCounter() {
var currCount = parseInt($('.counter').html());
$('.counter').text(currCount+1);
if (currCount+1 != max) {
setTimeout(incCounter,50);
}
}
</script>
</head>
<body>
<div class="counter">0</div>
</body>
</html>
I haven't tested this, but try...
function incCounter() {
var currCount = parseFloat($('.counter').html());
currCount += .01;
$('.counter').text( currCount.toFixed(2) );
if (currCount < max)
setTimeout(incCounter,50);
}
JS Fiddle to play with.