Javascript parseFloat parseInt - javascript

I'm Learning Javascript.
What does it mean to have parseFloat & parseInt functions in javascript, when in fact, Javascript does not differentiate between float and integer--there is only the Numeric data type.
Seems like they should have created parseNumber to be in alignment with their data types.
Any comments? Maybe somebody knows the reason. It just seems very odd.

parseInt("1.0") will be same as parseInt("1.1")
parseFloat("1.0") will be different to parseFloat("1.1")

In JS, there exist Number not parseNumber - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number. It works with numeric value, no matter whether it's float or int.
parseInt or parseFloat are generally used to parse string to number.
In decimal number i:e 10.3 the parseInt only return the number before decimal and discard everything after it, whereas with parseFloat it does consider the digits after decimal.
const num = 10.3;
console.log(parseInt(num));
console.log(parseFloat(num));

These functions are basically to parse string data to the relative format so according to the conditions we can use them
The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
The parseFloat() function parses an argument and returns a floating point number.
console.log(parseInt("1.11"))
console.log(parseFloat("1.11"))

parseFloat() and parseInt() is javascript functions which is basically use to convert string into Float or Integer number
var a = parseInt("10");
var b = parseInt("10.00");
var c = parseInt("10.33");
var d = parseInt("34 45 66");
var e = parseInt(" 60 ");
var f = parseInt("40 years");
var g = parseInt("He was 40");
var h = parseFloat("10");
var i = parseFloat("10.00");
var j = parseFloat("34 45 66");
var k = parseFloat("10.33");
var l = parseFloat(" 60 ");
var m = parseFloat("40 years");
var n = parseFloat("He was 40");
console.log("a : " + a);
console.log("b : " + b);
console.log("c : " + c);
console.log("d : " + d);
console.log("e : " + e);
console.log("f : " + f);
console.log("g : " + g);
console.log("h : " + h);
console.log("i : " + i);
console.log("j : " + j);
console.log("k : " + k);
console.log("l : " + l);
console.log("m : " + m);
console.log("n : " + n);

Related

Processing of string

I have following input for instance as a string:
(-4-1+255*4)^4
I also have some functions:
modulo(a, mod) => counts basic modulo
divideModulo (a, b, n) => divide operation for modulo
exponenatation(base, exp, mod) => exp operation for modulo etc.
I need to transform this string to:
exponenation(modulo(modulo(-4, mod) - modulo(-1, mod) + modulo(255, mod) * modulo(4, mod), mod), 2, mod)
Also string can contain / and square root and priority of signs must be preserved.
Right now, I am trying to do something like this:
var counter = 0;
var res = getNumbers(string); // return only numbers from string
var x = retezec.match(/\d+|\D/g); ( // returns array with numbers and string divided. Problem is // that "- digit" at the beginning is not added right now
for(var i = 0; i < x.length; i++){
if(!isNaN(x[i])){
x[i] = " modulo(" + res[counter] + ", " + mod + ") ";
counter++;
}
}
But it does not work as I need.
Thanks for help

Hex Number to Char using Javascript

How do I convert, for example, the string "C3" to its character using JavaScript? I've tried charCodeAt, toString(16) and everything, doesn't work.
var justtesting= "C3"; //there's an input here
var tohexformat= '\x' + justtesting; //gives wrong hex number
var finalstring= tohexformat.toString(16);
All you need is parseInt and possibly String.fromCharCode.
parseInt accepts a string and a radix, a.k.a the base you wish to convert from.
console.log(parseInt('F', 16));
String.fromCharCode will take a character code and convert it to the matching string.
console.log(String.fromCharCode(65));
So here's how you can convert C3 into a number and, optionally, into a character.
var input = 'C3';
var decimalValue = parseInt(input, 16); // Base 16 or hexadecimal
var character = String.fromCharCode(decimalValue);
console.log('Input:', input);
console.log('Decimal value:', decimalValue);
console.log('Character representation:', character);
Another simple way is to print "&#"+ CharCode like this
for(var i=9984;i<=10175;i++){
document.write(i+ " " + i.toString(16) + " &#" + i +"<br>");
}
OR
for(var i=0x2700;i<=0x27BF;i++){
document.write(i+ " " + i.toString(16) + " &#" + i +"<br>");
}
JSFIDDLE

Javascript - Don't understand data type of a variable

I have the following:
this.replaceBand1 = "1";
...
var replaceEndValue = Integer.valueOf(this.replaceBand1);
replaceBeginValue = replaceEndValue + 1;
//what is going on?
var type1 = typeOf replaceEndValue;
var type2 = typeOf replaceBeginValue;
log.debug("replaceEndValue: " + replaceEndValue + " " + type1);
log.debug("replaceBeginValue: " + replaceBeginValue + " " + type2);
...
setValue(column1, replaceEndValue);
setValue(column2, replaceBeginValue);
My expected outcome: replaceBeginValue will equal 2, and I can pass that into the function setValue that requires an Integer.
Actual outcome: replaceEndValue works, replaceBeginValue does not work.
The first debug shows - replaceEndValue: 1 object
The second debug shows - replaceBeginValue 2 string
I have no idea why replaceBeginValue is a string. Can anybody help?
You would need parseInt instead of Integer.valueOf. The function parseInt returns an int primitive type.
If you would like replaceBeginValue to equal 2, you would need something like this:
this.replaceBand1 = "1";
...
var replaceEndValue = parseInt(this.replaceBand1);
replaceBeginValue = replaceEndValue + 1;
...
Note: Integer.valueOf is for Java not JavaScript.

Here is a boolean-logic

Write a JavaScript function named findTen that reads two numbers from two text fields and then outputs to a div "True" if either one of them is 10 or both of them are 10 or if their sum is 10. Otherwise your function should output "False" to the div.
This is what I have so far, but it is not outputting correctly. Something is wrong with my else if statement:
<script>
function findTen() {
var a = document.getElementById("one").value;
var b = document.getElementById("two").value;
var doTheMath = a + b;
if ( a == 10 || b == 10) {
alert("true");
}
else if (doTheMath == 10 ) {
alert("true");
}
else {
alert(a b doTheMath);
}
document.getElementById("output").innerHTML = c;
}
</script>
There are a few errors in your posted code:
a and b are strings, so doTheMath is actually a string. In the case of a = 5 and b = 5, doTheMath is '55'. They need to be converted, in one of a number of ways. I chose Number:
var doTheMath = Number(a) + Number(b);
alert(a b doTheMath) is improper syntax. You should look to concat them:
alert(a + ' ' + b + ' ' + doTheMath);
c is undefined in your assignment at the end. So in your if/else blocks you probably want a statement like: c = false;
You can see all these problems fixed in this jsfiddle.
Your problem is that it handles the inputs as Strings and not as integers.
what you have to do is make the strings into integers by using the parseInt function
Your a and b variables are strings because you just got them out of text fields. In JavaScript, if you use the + operator on strings it will concatenate them. This is rather unfortunate 'feature' of javascript. To add the values from text fields you first need to convert them to integers:
var doTheMath = parseInt(a) + parseInt(b);
Furthermore, I don't think this statement will work at all:
alert(a b doTheMath);
Alert takes a string, so if you want to display those three values you'll need to concatenate them e.g.
alert(a + ' + ' + b + ' = ' + doTheMath);

JS - summing elements of arrays

I'm trying to make operations with numbers that are stored in arrays, unfortunately they seem to be considered as "text" rather than numbers, so when I do like array1[i] + array2[i], instead of doing 2+3=5 I would get 2+3=23 ...
The values of these arrays come from fields (html)
No idea how to deal with that ;D
Thanks for your help
If you wanna look at my code, here's what it looks like :
var polynome = [];
for (var i=0; i<=degree; ++i) {
polynome[i] = document.getElementById(i).value;
}
var a = document.getElementById("a").value;
var tempArray = [];
var quotient = [];
quotient[degree+1] = 0;
for (var i=degree; i>=0; --i) {
tempArray[i] = a * quotient[i+1];
quotient[i] = polynome[i] + tempArray[i];
}
document.getElementById("result").innerHTML = polynome + "<br>" + tempArray + "<br>" + quotient;
array1[i] contains string and not int so when you are trying both elements with + operator it is concatenating both the values rather of adding them
you need to cast both elements in arrays to integer
try this
parseInt( array1[i] ) + parseInt( array2[i] )
The + punctuator is overloaded an can mean addition, concatenation and conversion to number. To ensure it's interpreted as addition, the operands must be numbers. There are a number of methods to convert strings to numbers, so given:
var a = '2';
var b = '3';
the unary + operator will convert integers and floats, but it's a bit obscure:
+a + +b
parseInt will convert to integers:
parseInt(a, 10) + parseInt(b, 10);
parseFloat will convert integers and floats:
parseFloat(a) + parseFloat(b);
The Number constructor called as a function will convert integers and floats and is semantic:
Number(a) + Number(b);
Finally there is the unary - operator, but it's rarely used since subtraction (like multiplication and division) converts the operands to numbers anyway and it reverses the sign, so to add do:
a - -b

Categories

Resources