I'm having an issue in Internet Explorer 8, it's not computing addition of three variables in javascript?
I have this:
var y = function(s) {
var p = ($.browser.msie || $.browser.opera) ? h(s) : s.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
delete p[0];
var r = ((p[1] / 255) * 0.2989);
var g = ((p[2] / 255) * 0.5180);
var b = ((p[3] / 255) * 0.1140);
return (r + g + b);
};
Now, this is working in Chrome, Opera, and Firefox but not Internet Explorer, IE is giving me NaN. It's working if I just use return (r + g) or return (g + b)
I have tried return ((r + g) + b) and I have tried var rg = (r + g); return (rg + b);
But I keep getting NaN. Is there some special trick to adding three variables in javascript with IE???
I'd first try running isNaN() against each of the variables r,g and b to ensure that the value of each is in fact numeric. If so, perhaps try adding them together and assigning the result to a variable that you then return.
Related
This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed last year.
I am a beginner at javascript. I find following piece of code a bit difficult for me. I think both are same, and should have give the same result. But they are throwing different result.
var a = 5,
b = 6,
c = 7;
if (a + b > c || b + c > a || a + c > b) {
let s = (a + b + c) / 2;
let area = Math.sqrt(s * ((s - a) * (s - b) * (s - c)));
console.log(`The area of the triangle is: ${area}`);
} else {
console.log('Triangle does not exist');
}
It gives me a result. But when i try to take input from user and input the same value (5,6,7), find different result.
var a = prompt(),
b = prompt(),
c = prompt();
if (a + b > c || b + c > a || a + c > b) {
let s = (a + b + c) / 2;
let area = Math.sqrt(s * ((s - a) * (s - b) * (s - c)));
console.log(`The area of the triangle is: ${area}`);
} else {
console.log('Triangle does not exist');
}
Maybe I am wrong. But I just want know the reason.
Thanks.
My assumption is that this is related to prompt() returning a string and not an integer. Therefore the additions of the variables a, b and c would concatenate the strings and only after that - caused by the comparison > and after division / - convert it to a number. So in order to fix this, I think using parseInt() to parse the numbers returned from prompt() would fix the issue
The promt causes the variables to be defined as a string and as #Barmar pointed out in his comment, this changes the behavior of the '+' operation in your if-statement.
With numbers 1 +1 is returned as 2, with strings 11.
What you can do is change the following:
let s = (a + b + c) / 2;will give you 567/2 as you have strings.
let s = (parseInt(a) + parseInt(b) + parseInt(c) ) / 2; will give you the desired result as the method parseInt() returns a intger value.
Is there any way to deobfuscate some javascript code that produced with webpack 4 and is also splitChunked?
It's a little more than 1MB js code and I only need to understand a small portion of the code, which is this function :
function l(e) {
t.d(8, function(e) {
for (var n = e.length, r = t.b(n), f = a(), c = 0; c < n; c++) {
var i = e.charCodeAt(c);
if (i > 127)
break;
f[r + c] = i
}
if (c !== n) {
0 !== c && (e = e.slice(c)),
r = t.c(r, n, n = c + 3 * e.length);
var d = a().subarray(r + c, r + n);
c += o(e, d).written
}
return u = c,
r
}(e), u);
var n, r, f = (null !== i && i.buffer === t.e.buffer || (i = new Int32Array(t.e.buffer)),
i), c = (n = f[2],
r = f[3],
d.decode(a().subarray(n, n + r))).slice();
return t.a(f[2], 1 * f[3]),
c
}
I used chrome debugger and set some breakpoints and I was able to grasp what it's doing but I need to do the exact same thing in my project So I need a more readable code to do that.
As far as I know, there is no easy way to do so, but I want to share some tips with you:
Replace all ',' with ', '\n'
Review and correct the possible code breaks
Use a for loop to iterate between static value arrays and replace all the usages.
Change the encoding in case of language dependent breaks
Replace all ; with ; \n and correct possible code breaks
Know it is easier to rename functions and write comments for them.
For a uni project I am making my own RGB to HSV colour converter.
Here is the code I have written
function calcSat() {
s = (maxRGB() - minRGB()) / maxRGB();
if (isNaN(s)) {
s = 0;
}
}
function calcVal() {
v = maxRGB();
}
function calcHue() {
var dr = (maxRGB() - r) /
maxRGB() - minRGB();
dr = Math.abs(dr);
var dg = (maxRGB() - g) /
maxRGB() - minRGB();
dg = Math.abs(dg);
var db = (maxRGB() - b) /
maxRGB() - minRGB();
db = Math.abs(db);
console.log("red:" + r, "green:" + g, "blue:" + b);
console.log("red d:" + dr, "green d:" + dg, "blue d:" + db);
if (s == 0) {
h = 0;
} else if (r == maxRGB && g == minRGB) {
h = 5 + db;
} else if (r == maxRGB && g != minRGB) {
h = 1 - dg;
} else if (g == maxRGB && b == minRGB) {
h = dr + 1;
} else if (g == maxRGB && b != minRGB) {
h = 3 - db;
} else if (r == maxRGB) {
h = 3 + dg;
} else {
h = 5 - dr;
}
h = h * 60;
}
You can also access a copy of the project here: https://editor.p5js.org/eeu8cc/sketches/tYfnkWrA-
Essentially, I am trying to create 3 functions to calculate the Hue, Saturation and Value of an RGB colour. Everything runs fine but my output is completely wrong.
As far as I can tell I have reproduced the equation correctly.
Here is the equation to that I am trying to re-write in code:
For those interested, the screenshots came from a PDF that can be found here.
Any help would be greatly appreciated.
Why are you taking absolute values for dr,dg,db? Not that there should be any way for these to go negative, and in fact if they do I would be taking that as indicating a bug further up.
I am a C and VHDL man rather then javascript, but surely
if (r == maxRGB && g == minRGB)
should rather be something like
if (r == maxRGB() && g == minRGB())?
I am assuming that == & && bind the right ways and that you don't need extra braces (I would put them in in C just because it has some obscure cases that are not worth remembering).
I also question the wisdom of floating point equality comparisons here, that seldom ends well. It would be ok, if not great design in a fixed point type but IIRC javascript tends to use FP for any numeric stuff which is just painful (And X86 does excess precision if there is not a register flush involved, which can sting with FP equalities). See Goldburgs classic paper if you want to get your math on, or this if you want something simpler https://floating-point-gui.de/
Should not maxRGB and minRGB be taking arguments (You don't show these functions) but I would expect them to take r,g,b as parameters?
I tend to deal with colour in broadcast (where we live in Y'CbCr spaces), but I also think you should probably be converting to a linear light space before doing your conversion as otherwise the gamma curves will screw you, but this is a nicety.
https://jsfiddle.net/2L4t9saq/180/ is my fiddle
most of the code is just useless, ill just post the stuff that matters
var baseConverter = function(r, e, n) {
var o = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (e <= 0 || e > o.length || n <= 0 || n > o.length) return console.log("Base unallowed"), null;
var l, t = 0;
if (10 != e) {
var a = r.length;
for (l = 0; l < a; l++) {
var u, f = -1;
for (u = 0; u < o.length; u++)
if (r[l] == o[u]) {
f = 1;
break
}
if (u >= e) return console.log("Symbol unallowed in baseform"), null;
if (-1 == f) return console.log("Symbol not found"), null;
var s = a - l - 1;
t += 0 == s ? u : u * Math.pow(e, s)
}
} else t = parseInt(r);
if (10 != n) {
for (var g = []; t > 0;) {
var i = t % n;
if (i < 0 || i >= o.length) return console.log("Out of bounds error"), null;
g.push(o[i]), t = parseInt(t / n)
}
return g.reverse().toString().replace(/,/g, "")
}
return t.toString()
}
var b36torgba = function(input) {
for (var i = 1; i < (input.length / 8) + 1; i++) {
var arr = input
var r = arr.charAt[0 + (i - 1) * 8] + "" + arr.charAt[1 + (i - 1) * 8]
var g = arr.charAt[2 + (i - 1) * 8] + "" + arr.charAt[3 + (i - 1) * 8]
console.log(g.charAt[2])
var b = arr.charAt[4 + (i - 1) * 8] + "" + arr.charAt[5 + (i - 1) * 8]
console.log(b)
var a = arr.charAt[6 + (i - 1) * 8] + "" + arr.charAt[7 + (i - 1) * 8]
console.log(a)
var rrgba = baseConverter(r, 36, 10)
var grgba = baseConverter(r, 36, 10)
var brgba = baseConverter(r, 36, 10)
var argba = baseConverter(r, 36, 10)
var bigMessOfAVariable = "rgba(" + rrgba + "," + grgba + "," + brgba + "," + argba + "),"
return bigMessOfAVariable;
}
}
you can ignore the top function, all it is is a base converter script, that takes in three inputs, an input, the base its in, and the base it should be converted to: eg baseConverter(73,36,10) will output 255.
now, the problem is with my b36torgba function.
it will take in a string, which is guaranteed to have a length that is either 0, 8, or a multiple of 8, this is just standardization to make sure everything runs smoothly, without having 700 indexOf[] functions.
it takes in the input, and divides it by 8, this tells the function how many bytes it has to go through, and how many it will spit out, so a string "[7300002S7300002S]" should (divided by 8) output 2, therefore the script runs 2 iterations.
currently, it should be taking in the string, and assigning each group of 2 characters (again standard) to a specific variable, this will allow it to all be put in the end and outputted as the same string but in base 10 rgba (hence 73 being use, 73 in base 36 is 255), but before it can do any of that, it breaks when it tries to find the characters in a string, saying this syntax error:
Uncaught TypeError: Cannot read property '0' of undefined
at b36torgba ((index):40)
at window.onload ((index):55)
why does it break as soon as it tries to feed the string into my charAt()'s?
ps: i do understand that the code in its current state, if it worked, it'd only output the rgba value of the last 8 characters
Easy mistake. You're using charAt (which is a function) by doing charAt[index] (using square brackets), rather than charAt(index) (using round brackets). Fixing that up should solve your issue.
Also - you're calling the function by doing b36torgba(["7300002S7300002S"]) in your JSFiddle, and trying to do string manipulation on it. Since ["7300002S7300002S"] is an array, not a string, .charAt() won't work on it. Try calling the function by doing b36torgba("7300002S7300002S") instead.
function getAnswer(){
var answer, c = 334;
while (c < 999){
var a = Math.round(((1000 - c) / 2) - 0.5), b = Math.round((1000 - c) / 2);
while (a > 0 && b < c){
if (Math.pow(a, 2) + Math.pow(b, 2) != Math.pow(c, 2)){
a -= 1;
b += 1;
}else{
answer = a * b * c;
}
}
c += 1;
}
document.getElementById("a").innerHTML = answer;
}
Hi! I am a beginner programmer in javascript, and I have been trying to solve problem 9 in Project Euler. That problem goes like this:
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
I don't know why no answer appears, and my script crashes/program stops running, whenever I run this script. Please explain and tell me what's wrong with my script.
When you have found the answer, you don't stop with the iteration. Even worse, you don't change the values of a and b any more, so they never reach the end of the iteration, and you're stuck in an infinite loop.
You'll need to break out of the loop when you've found the answer. Or even break out of both your nested loops, using a label:
function getAnswer() {
var answer,
c = 334;
find: while (c < 999) {
var a = Math.round(((1000 - c) / 2) - 0.5),
b = Math.round((1000 - c) / 2);
while (a > 0 && b < c) {
if (Math.pow(a, 2) + Math.pow(b, 2) == Math.pow(c, 2)) {
answer = a * b * c;
break find;
}
a -= 1;
b += 1;
}
c += 1;
}
document.getElementById("a").innerHTML = answer;
}
Notice that it would be easier if your function just returned the answer, instead of populating #a with it. You'd call it like
document.getElementById("a").innerHTML = getAnswer();
and can just return a * b * c; to break out of the whole function.