Javascript If or statement [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to write a simple JavaScript statement.
if ($(window).scrollTop() > 400 ) || if (!($(window).scrollTop() + $(window).height() > $(document).height() - 500 )) {
$nav.addClass('show');
}
Basically if user scrolls more than 400 from top and more than 500 from bottom I want to add the class show

To solve a problem like this, you need to do something called "debugging". There are many individual debugging techniques. One basic one is to keep replacing things with smaller pieces, or removing pieces, until the problem goes away, or you get an error you can understand more easily.
With your statement
if ($(window).scrollTop() > 400 ) || if (!($(window).scrollTop() + $(window).height() > $(document).height() - 500 )) {
$nav.addClass('show');
}
you will get an error in the console. Open the console and view the error. (If you don't know what the console is, drop everything you are doing and learn that before you do anything else.) Let's say you can't figure out what the problem is from the error message.
In that case, replace the first item in the if condition with a simple true:
if (true || if (!($(window).scrollTop() + $(window).height() > $(document).height() - 500 )) {
$nav.addClass('show');
}
You will continue to get an error. That tells you that the problem was not in the
($(window).scrollTop() > 400 )
portion of the condition. Let's say that you still can't understand the error message, or figure out what to do about it. So next, replace the second part of the condition with another true:
if (true || if (true)) {
$nav.addClass('show');
}
At this point, you will get a console error message saying:
Uncaught SyntaxError: Unexpected token if
It is very unlikely that the initial if is causing this problem; that's obviously valid JavaScript syntax. So the problem must be the second if inside the condition. You can now jump to the conclusion that you do not need to, indeed must not, place a second if statement inside the condition. Remove it. Your problem is solved, without depending on generous SO people to identify every syntax error in your program, and you can move on and complete your project.

Related

Javascript false control [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a problem I want the script not to run unless I am less than 5 trainqueue_barracks
but I put 100 here and it doesn't work either. What's wrong?
if ( document.document.getElementById("trainqueue_barracks") == null
|| document.document.getElementById("trainqueue_barracks").rows.length < 5
) {
As you have tagged jquery, you can simply do it like this:
if(!$('#trainqeueu_barracks').length || $('#trainqeueu_barracks tr').length < 5)
{
...
}
Note that !$('#trainqeueu_barracks').length will be always true in your case, so the second part won't actually matter. I think what you really need is:
if($('#trainqeueu_barracks tr').length < 5)
{
...
}
First off, you shouldn't need the first condition of your loop
document.document.getElementById("trainqeueu_barracks") == null
Also if you put 100 in their then it shouldn't work and your code is right. Unless you want it to run when rows.length is >= 5.
document.document.getElementById("trainqeueu_barracks").row.length >= 5
Is the opposite end of what you have.

Javascript appears to be saying true == false [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have the following js function.
function showAttentionItem(sender)
{
debugger;
var assistanceRequired = sender.parent().hasClass("assistanceRequired");
if (assistanceRequired)
{
sender.children('.assistanceRequiredText').fadeToggle(0);
if (sender.children('.assistanceRequiredText').is(":visible"))
{
sender.children('.studentPerformanceText').hide();
}
}
if (!assistanceRequired)
{
if (sender.parent().hasClass("studentOutsideTargetRange"))
{
sender.children('.studentPerformanceText').fadeToggle(0);
}
}
}
What happens when I run it is, I hit the debugger line, and step through the code. assistanceRequired is true.
After executing sender.children('.studentPerformanceText').hide();, the next line it jumps to is sender.children('.studentPerformanceText').fadeToggle(0);, inside the if (!assistanceRequired) statement!
How could this possibly be happening?
This is guaranteed to work:
if (sender.parent().hasClass("assistanceRequired"))
{
// do whatever here
}
else
{
// do whatever ELSE here
}
Only one or the other can ever run when coded correctly since there is only one test and it will either be true or false the single time it is tested. There is no possibility for it to be changed by some side effect you are not aware of, which is most likely what is happening. This is not debatable.
If this does not work the way you want then something else is wrong that you are not showing and without an executable jsFiddle good luck convincing anyone that a fundamental thing like the if ... else construct is broken.
I can think of only one scenario to explain what you are seeing.
There's a double trigggering of showAttentionItem(), which, in the debugger, is perceived as a single call. On the first call, assistanceRequired is true and on the second it's false.

Unexpected token ! within if statement [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
this is my markup
http://i.imgur.com/j2rhUH3.png
I tried $('.post-form').find('p:first').text().indexOf("Logged") !=''), and try to detect whether Logged is set or not, but I got this error Unexpected token !
You get error Unexpected token ! because your statement
$('.post-form').find('p:first').text().indexOf("Logged") !='')
is not complete.
Use if condition and indexOf as shown below :-
if( $('.post-form').find('p:first').text().indexOf("Logged") > -1)
The indexOf() method returns the position of the first occurrence of a specified value in a string.
This method returns -1 if the value to search never occurs
indexOf returns integer not string.
It returns the position where the specified searchvalue occurs for the first time, or -1 if it never occurs.
You get error Unexpected token ! because your statement
$('.post-form').find('p:first').text().indexOf("Logged") !='')
is incomplete.
Following should work:
if($('.post-form').find('p:first').text().indexOf("Logged") >= 0){
...
}
See DEMO here.

"Expected token: '}'" Jquery JavaScript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am getting an "Expected token: '}'" and when I add that token to the requested line i get "Unexpected token: '}'". Not really sure what I did wrong.
Any input is appreciated! Thanks!
$(document).ajaxSuccess(function(){
var currentPrice = $.trim($("#ProductPrice").text());
if(currentPrice == "%%GLOBAL_ProductPrice%%")
{
$("#ProductPrice").css('color','black');
$("#ProductPrice").removeClass("PriceChanged")
}
else
{
var scrollPos = $(window).scrollTop()
if(scrollPos >= 397) {
$("#ProductPrice").css('color','red');
$('html, body').animate({
scrollTop: $("#ProductPriceWrap").offset().top
}, 1000);
$("#ProductPriceWrap").animate({backgroundColor: "#ff0000" });
$("#ProductPriceWrap").animate({backgroundColor: "#ffffff" });
$("#ProductPrice").addClass("PriceChanged");
}
else
{
$("#ProductPriceWrap").animate({backgroundColor: "#ff0000" });
$("#ProductPriceWrap").animate({backgroundColor: "#ffffff" });
$("#ProductPrice").addClass("PriceChanged");
}
};
You last closing } actually closes this line:
if(currentPrice == "%%GLOBAL_ProductPrice%%")
{
However the expression that starts here, never gets properly closed:
$(document).ajaxSuccess(function(){
So add a }); to the end and you should be good.
Also, let this be a lesson for you in the value of consistent and proper indentation. If you paste that code into http://jsbeautifier.org/ you would see the the last line is not at an even indent level as the first line. It becomes pretty obvious then where the problem is.
If the code within that callback function was all indented, you probably would have noticed this and it would have been a quick fix.
So today you learned the value of consistent and proper indentation.

I cannot find this syntax error [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm going to feel like a complete idiot once this is pointed out to me, but I've got a syntax error I cannot figure out where the issue is coming from. Here is my code (error appears on last line but I doubt its that line that caused that):
// handle GCM notifications for Android
function onNotificationGCM(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
PushWoosh.appCode = "33F93-5013B";
PushWoosh.register(e.regid, function(data) {
alert("PushWoosh register success: " + JSON.stringify(data));
}, function(errorregistration) {
alert("Couldn't register with PushWoosh" + errorregistration);
});
}
break;
Thanks guys, I'm feeling like an idiot here and had a frustrating day.
Your onNotificationGCM() function is not closed, and neither is the switch block contained within it. The JavaScript parser is expecting to see two additional close braces (}) but the input file terminates before they are seen.
My guess is that you need to add these two braces after your break; statement, prior to the assignment of PushNotification.prototype.register.

Categories

Resources