How to strip 25 to just 5 using javascript? - javascript

if i have a number like 25 for example, i would like to use javascript to "knock off" the 2 and keep the 5.
Same for any number - i want the 9 from the 89.
But i also want it to not do this if the number is single like an 8 for example
Thanks in advance,
Reece

Use the modulo operator, it returns the "rest" after a division:
lastDigit = number % 10;

You could use modulus:
var number = 89;
var reminder = number % 10; // 9

You can use the modulus operator (%):
var num = 89 % 10; // 9

var number = 25;
var singledigit = number%10;

Related

How to round a whole number in javascript

How to round an integer in javascript to its previous decimal.
Ex.:
15 to 10.
16 to 10.
21 to 20.
29 to 20.
Can even use Math.floor()
var num = 24;
var round = Math.floor(num / 10) * 10;
console.log(round)
This should do the trick
parseInt(15/10) * 10
you can create your own function to do that
function intFloor(num){
let temp = num%10;
return num-temp;
}
console.log(intFloor(15));
if you know prototype You can add this to prototype
Number.prototype.intFloor = function(){
let temp = this%10;
return this - temp;
}
console.log((52).intFloor()) // result 50

How do I solve this ? Javascript

Here is the problem:
Declare a variable called x and assign it a value that equals the remainder of 20 divided by 3. Next, divide and assign 1 to that variable and display the result in an alert box. The result should be 2.
Here is my code:
var x = 20 / 3;
alert(x / 1);
I am brand new to Javascript. I am coming up with 6.66. The answer should be 2. What am I doing wrong?
You need this:
the remainder of 20 divided by 3
You're dividing. The remainder (or modulo) operator in JavaScript is the percentage symbol (%).
So your code should look like this instead:
var x = 20 % 3;
alert(x / 1);
Further reading:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
https://www.w3schools.com/js/js_arithmetic.asp
Well there is % in JS for remainder. / is the division sign.
var x = 20 % 3; console.log(x / 1);

javascript: Base 10 binary to base 10

I am trying to take Binary base 10 actually binary but interpreted as base 10 and convert it to base 10 ex: 100 interpreted as 1*10^2 but I want to be 4*10^0
You can use parseInt(string, radix)
parseInt('1100', 2) //will be 12
Or in ES6 you can use 0b
var foo = 0b1100;
foo === 12; //true
You can do this like this var a = parseInt(100, 2);
Now a = 4;
I mean, if you have number n based 10 but actually based 2, then you convert it to based 10, by this: var a = parseInt(n, 2);

Round a float up to the next integer in javascript

I need to round floating point numbers up to the nearest integer, even if the number after the point is less than 0.5.
For example,
4.3 should be 5 (not 4)
4.8 should be 5
How can I do this in JavaScript?
Use the Math.ceil[MDN] function
var n = 4.3;
alert(Math.ceil(n)); //alerts 5
Use ceil
var n = 4.3;
n = Math.ceil(n);// n is 5
Round up to the second (0.00) decimal point:
var n = 35.85001;
Math.ceil(n * 100) / 100; // 35.86
to first (0.0):
var n = 35.800001;
Math.ceil(n * 10) / 10; // 35.9
to integer:
var n = 35.00001;
Math.ceil(n); // 36
jsbin.com
Use
Math.ceil( floatvalue );
It will round the value as desired.

get the number of n digit in a 2+ digit number

For example, getting "5" in "256". The closest I've gotten is Math.floor(256/10)), but that'll still return the numbers in front. Is there any simple way to get what I want or would I have to make a big function for it? Also, for clarity: "n digit" would be defined. Example, getDigit(2,256) would return 5 (second digit)
Math.floor((256 / 10) % 10)
or more generally:
Math.floor(N / (Math.pow(10, n)) % 10)
where N is the number to be extracted, and n is the position of the digit. Note that this counts from 0 starting from the right (i.e., the least significant digit = 0), and doesn't account for invalid values of n.
how about
(12345 + "")[3]
or
(12345 + "").charAt(3)
to count from the other end
[length of string - digit you want] so if you want the 2 it's:
5 - 4 = 1
(12345 + "")[1] = "2"
function getNumber (var num, var pos){
var sNum = num + "";
if(pos > sNum.length || pos <= 0){return "";}
return sNum[sNum.length - pos];
}
First, you need to cast the number to a string, then you can access the character as normal:
var num = 256;
var char = num.toString()[1]; // get the 2nd (0-based index) character from the stringified version of num
Edit: Note also that, if you want to access it without setting the number as a variable first, you need a double dot .. to access the function:
var char = 256..toString()[1];
The first dot tells the interpreter "this is a number"; the second accesses the function.
Convert to string and substring(2,2)?
This should do it:
function getDigit ( position, number ) {
number = number + ""; // convert number to string
return number.substr ( position + 1, 1 ); // I'm adding 1 to position, since 0 is the position of the first character and so on
}
Try this, last line is key:
var number = 12345;
var n = 2;
var nDigit = parseInt((number + '').substr(1,1));
If you want to try to do everything mathematically:
var number = 256;
var digitNum = 2;
var digit = ((int)(number/(Math.pow(10,digitNum-1))%10;
This code counts the digit from the right starting with 1, not 0. If you wish to change it to start at 0, delete the -1 portion in the call.
If you wish to count from the left, it gets more complicated and similar to other solutions:
var number = 256;
var digitNum = 2;
var digit = ((int)(number/(Math.pow(10,number.tostring().length-digitNum))%10;
edit:
Also, this assumes you want base 10 for your number system, but both of those will work with other bases. All you need to do is change instances of 10 in the final line of code to the number representing the base for the number system you'd like to use. (ie. hexadecimal =16, binary = 2)
// You do not say if you allow decimal fractions or negative numbers-
// the strings of those need adjusting.
Number.prototype.nthDigit= function(n){
var s= String(this).replace(/\D+/g,'');
if(s.length<=n) return null;
return Number(s.charAt(n))
}
use variable "count" to control loop
var count = 1; //starting 1
for(i=0; i<100; i++){
console.log(count);
if(i%10 == 0) count++;
}
output will fill
1
2
3
4
5
6
7
8
9

Categories

Resources