Variable not changing on if statement [duplicate] - javascript

This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
I am working on code for a site am trying to take an input and based on the input display a specific message. Whenever I type in a number however it is being set to the first if result. Any help would be great, thanks
<form name="testsDone">
How many of your tests have you taken? <input type ="text" id="tookTests" /><br />
</form>
<script>
function testCheck(){
var a = parseFloat(document.getElementById("tookTests").value);
var b = a;
var c;
if (b = 2 ) {
c = "Yes you can! You've earned it.";
} else if (b = 1) {
c = "You need to keep studying then";
}
else if (b > 2 ) {
c = "That's more tests than you're supposed to have";
}else {
c = "Why are you on here? You need to study.";
}
document.getElementById("change").innerHTML = c;
}
</script>
<button onclick="testCheck()">Check and See</button>
<p> Can you go to sleep?</p>
<p id="change"></p>

You are assigning value here..
if (b = 2 ) {
it should be..
if (b == 2 ) {

Hey change it to the following
function testCheck(){
var a = parseFloat(document.getElementById("tookTests").value);
var b = a;
var c;
if (b == 2 ) {
c = "Yes you can! You've earned it.";
} else if (b == 1) {
c = "You need to keep studying then";
}
else if (b > 2 ) {
c = "That's more tests than you're supposed to have";
}else {
c = "Why are you on here? You need to study.";
}
document.getElementById("change").innerHTML = c;
}

Related

Comparing 3 identical values for equality using Javascript [duplicate]

This question already has answers here:
Javascript compare 3 values
(7 answers)
Closed 2 years ago.
I am working on studying for an entry test, and being self learned I have been working a lot of functions problems. this one has stumped me,
I a to write a function testing to see if 3 values are equal. The code i have tired is:
Function equal(a,b,c){
return a==b==c;
}
as well as:
function equal(a,b,c){
let newEqual=a==b;
return newEqual===c
}
I feel like I am missing something rather simple but have not been able to put my finger on it.
thank you in advance for any insight
a == b == c will be evaluated as :
a == b then checks the result ( true ) and compares it with c => true == c which is false :
const a = 5;
const b = 5;
const c = 5;
const result = a == b == c ;
console.log(result); // false
const a1 = 5;
const b1 = 5;
const c1 = true;
const result1 = a1 == b1 == c1 ;
console.log(result1); // true
You should compare them separately :
const a = 5;
const b = 5;
const c = 5;
const result = a == b && b == c ;
console.log(result);
To check whether all three variables are equal or not, use && operator to add on queries.
&& returns true if all conditions are true.
function equal(a,b,c){
return a == b && b == c;
}
As mentioned by #mplungjan, for strict comparison use === instead of ==.
Try this.
function equal(a,b,c){
return a==b && b==c;
}
You can check it by doing the following:
function equal(a,b,c){
return (a==b) && (b==c)
}
That way you are checking if a == b is true, and b == c is true then all three are equal. In other words, true && true = true

Chained logical OR comparison [duplicate]

This question already has answers here:
Javascript if statement with multiple permissible conditions [duplicate]
(4 answers)
Closed 5 years ago.
Why doesn't this work:
if (x != (a || b || c)) {
doStuff();
}
It's meant to check whether x is NOT equal to a OR b OR c.
EDIT: How would I achieve a check for whether x is NOT equal to a OR b OR c?
EDIT: Ok, it's a duplicate. What do I do now, take minus points even after realizing my mistake? :P
To use multiples values like you wanna just:
var x = 'x';
var a = 'a';
var b = 'b';
var c = 'c';
function doStuff() {
console.log(1)
}
// exemple 1
if(x == a || x == b || x == c) {
doStuff();
}
function isSameValue(element, index, array) {
return element === x;
}
// exemple 2
if([a, b, c].some(isSameValue)) {
doStuff();
}
// exemple 3
[a, b, c].includes(x);

multiple if but only runs the first one? javascript

so I wrote this for my first computer science class assignment. However, the page returns that if input is gpa(A), the results is 3. It's like only the first conditionals if is running. I switched around the A, B ,C and 2, 3, 4 but it's always the first if no matter what the gpa(r) is. How does this happen?
var gpa = function(r) {
if (r = "B"){
return 3;
}
if (r = "C"){
return 2;
}
if (r = "A"){
return 4;
}
}
In order to compare between two values you should use == or === and not = which assigns a value.
var gpa = function(r) {
if (r == "B"){
return 3;
}
if (r == "C"){
return 2;
}
if (r == "A"){
return 4;
}
}
Read here about the difference between == and ===.

How to ask a user to guess a number between 1 - 1000

I'm trying to write some javascript code that asks the user to guess a number from 1 to 1000 and enter it into the prompt box. If the user guesses right, an alert box will pop up saying they got it right. If they guess wrong, another alert box will popup and say that they are wrong and to try once more.
The issue here is that I don't know what I have to do to make the code loop infinitely until they get the right answer. Here's what i have so far:
var a = 489; // the number that needs to be guessed to win the game.
//var b stores whatever value the user enters.
var b = prompt("Enter a number in between 1 and 1000");
// if/else statement that test if the variables are equal.
if (b == a) {
alert("You're right!");
} else {
alert("Incorrect! Try again!");
}
Number matching
Basically, when you make prompt, it returns a String or text, not a number. To fix this, do:
if (parseInt(b,10) === a) {
//Code
}
Other ways
They're a lot of ways to parse numbers. Here's a few more:
parseFloat(b); // Also parses decimals: '5.3' -> 5.3
parseInt(b, 10); // Gives an integer (base 10): '5.3' -> 5
+b; // A very 'short' way; '5.4' -> 5.4
Number('5.4e2'); // Number case: '5.4e2' -> 540
Looping
Now to repeat? Make it a loop!
var a = 432;
while (true) {
var b = prompt("Enter a number in between 1 and 1000");
if (b == a){
alert("You're right!");
break; // Stops loop
} else if (!b) { break; }
else {
alert("Incorrect! Try again!");
}
}
Not sure why, but some people hate while true loops. They shouldn't cause any problems as long as you coded it properly
Random Numbers
You can get a random number using Math.random.
var min = 1,
max = 1000;
Math.floor(Math.random() * (max - min + 1)) + min;
If you're like me and want short code, you can shorten it by:
Math.floor(Math.random() * (999)) + 1;
All Together Now!
var a = Math.floor(Math.random() * (999)) + 1;
while (true) {
var b = prompt("Enter a number in between 1 and 1000");
if (b == a) {
alert("You're right!");
break; // Stops loop
} else if (!b) {
alert("The Correct Answer was: " + a); //Shows correct answer
break;
} else {
alert("Incorrect! Try again!");
}
}
Just stick your prompt in some kind of loop. The code will inside the loop will run over and over until the comparison is false.
Basic example:
http://jsfiddle.net/s2he1twj/
var a = 500,
b;
while (parseInt(b) !== a) {
b = prompt('Enter a number!');
if (b === null) break;
}
while loop
Do this recursively by calling the same function like
var a = 489;
function promptGuess(){
//var b stores whatever value the user enters.
var b = prompt("Enter a number in between 1 and 1000");
// if/else statement that test if the variables are equal.
if (b == a){
alert("You're right!");
} else {
alert("Incorrect! Try again!");
promptGuess();
}
}
promptGuess();
Use a while until the match:
var a = 489;
var b;
while(b != a) {
var b = prompt("Enter a number in between 1 and 1000");
if (b == a) {
alert("You're right!");
} else {
alert("Incorrect! Try again!");
}
}
One more thing: although the b != a evaluation is correct, it's error-prone. The != operator do conversion type, whilst the !== operator compares types and value.
In your case: 5 != '5' will return false, but 5 !== '5' returns true. It's recommended that your comparisons be conversion-free. It's more strict.
In your case, this means:
while(parseInt(b) !== a)
Greetings!

visual studio vs filezilla for publishing website [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My first Question:Which is better publish a web site from visual studio,or upload it by filezilla?
I didn't try any of them but someone told me when using filezilla:when I try to access my website, i get a file listing and when i try to view one of them, i just get a html text version of my pages.
but then again he said filezilla keeps the website even if it goes offline,is visual studio also the same?
And my SECOND Questin:
This is a jquery or javascript function?am I right?(which one?)
I saw it in a source code of a site,why does it look strange?I can't understand it,it has been encrypted?how can I turn it to sth which is more understandable?
(function (a, b) {
function c(a) { return K.isWindow(a) ? a : a.nodeType === 9 ? a.defaultView || a.parentWindow : !1 } function d(a) { if (!cl[a]) { var b = H.body, c = K("<" + a + ">").appendTo(b), d = c.css("display"); c.remove(); if (d === "none" || d === "") { cm || (cm = H.createElement("iframe"), cm.frameBorder = cm.width = cm.height = 0), b.appendChild(cm); if (!cn || !cm.createElement) cn = (cm.contentWindow || cm.contentDocument).document, cn.write((H.compatMode === "CSS1Compat" ? "<!doctype html>" : "") + "<html><body>"), cn.close(); c = cn.createElement(a), cn.body.appendChild(c), d = K.css(c, "display"), b.removeChild(cm) } cl[a] = d } return cl[a] } function e(a, b) { var c = {}; return K.each(cr.concat.apply([], cr.slice(0, b)), function () { c[this] = a }), c } function f() { cs = b } function g() { return setTimeout(f, 0), cs = K.now() } function h() { try { return new a.ActiveXObject("Microsoft.XMLHTTP") } catch (b) { } } function i() { try { return new a.XMLHttpRequest } catch (b) { } } function j(a, c) { a.dataFilter && (c = a.dataFilter(c, a.dataType)); var d = a.dataTypes, e = {}, f, g, h = d.length, i, j = d[0], k, l, m, n, o; for (f = 1; f < h; f++) { if (f === 1) for (g in a.converters) typeof g == "string" && (e[g.toLowerCase()] = a.converters[g]); k = j, j = d[f]; if (j === "*") j = k; else if (k !== "*" && k !== j) { l = k + " " + j, m = e[l] || e["* " + j]; if (!m) { o = b; for (n in e) { i = n.split(" "); if (i[0] === k || i[0] === "*") { o = e[i[1] + " " + j]; if (o) { n = e[n], n === !0 ? m = o : o === !0 && (m = n); break } } } } !m && !o && K.error("No conversion from " + l.replace(" ", " to ")), m !== !0 && (c = m ? m(c) : o(n(c))) } } return c } ...
it countinues
Your friend told you non-sense or you understood it that way. There's more than plain file transfer in VS deploy solution (like compilation of your source code into a dll for example), that can be fine tuned and so. Filezilla on its side is just plain ftp : you send the files you wish to a server.
You're not supposed to ask 2 questions at the same time, but still, to your second question :
this looks a lot like javascript, and it probably was minimized. This is a technique in which a parser replaces variable names by short names (letters), removes whitespaces and line feeds, etc in order to make the file smaller. Oh and JQuery is actually a javascript framework so there's no big difference between both.

Categories