473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How can this code be wrong

I've a simple if statement yet it executes even if condition is false.
What can be wrong?

var j = -1;
for (var i in files) {
var f = files[i].split('/', 1);
alert (j+','+i+','+(j == -1) || (folders[j] != f));
if ((j == -1) || (folders[j] != f)) {
alert (j+','+i);
j += 1;
folders[j] = f;
}
};

The second alert is shown even if the first shows condition = false.

O. Wyss
Apr 26 '07 #1
5 1103
On Apr 26, 8:00 pm, Otto Wyss <otw...@bluewin.chwrote:
I've a simple if statement yet it executes even if condition is false.
What can be wrong?

var j = -1;
for (var i in files) {
var f = files[i].split('/', 1);
alert (j+','+i+','+(j == -1) || (folders[j] != f));
if ((j == -1) || (folders[j] != f)) {
alert (j+','+i);
j += 1;
folders[j] = f;
}
};

The second alert is shown even if the first shows condition = false.

O. Wyss
how can j ever not be -1 in the code above, so the || condition is
always true, and when j is manually changed to say -2, the second
alert doesnt show.

Apr 26 '07 #2
wyo
On Apr 26, 11:53 pm, shimmyshack <matt.fa...@gmail.comwrote:
On Apr 26, 8:00 pm, Otto Wyss <otw...@bluewin.chwrote:
if ((j == -1) || (folders[j] != f)) {
alert (j+','+i);
j += 1;
folders[j] = f;
}
The second alert is shown even if the first shows condition = false.
O. Wyss

how can j ever not be -1 in the code above, so the || condition is
always true, and when j is manually changed to say -2, the second
alert doesnt show.- Hide quoted text -
j isn't alway -1 since after the first true condition j is set to 0 in
"j += 1". Besides the alert before really shows "false" for the full
condition.

O. Wyss

Apr 27 '07 #3
wyo
Figured out what was wrong, string compares works only with strings.

if ((j == -1) || (String(folders[j]) != String(f))) {

O. Wyss

Apr 27 '07 #4
wyo wrote:
Figured out what was wrong, string compares works only with strings.

if ((j == -1) || (String(folders[j]) != String(f))) {

O. Wyss
For the record, what were you parsing? My guess was arrays you
populated via server-side code.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Apr 27 '07 #5
On Apr 26, 12:00 pm, Otto Wyss <otw...@bluewin.chwrote:
I've a simple if statement yet it executes even if condition is false.
What can be wrong?

var j = -1;
for (var i in files) {
var f = files[i].split('/', 1);
alert (j+','+i+','+(j == -1) || (folders[j] != f));
if ((j == -1) || (folders[j] != f)) {
alert (j+','+i);
j += 1;
folders[j] = f;
}
};

The second alert is shown even if the first shows condition = false.
You may wish to consider why the first alert:

alert (j+','+i+','+(j == -1) || (folders[j] != f));

gives a different result from:

alert (j+','+i+','+((j == -1) || (folders[j] != f)));

and decide which one would be the correct one to use.

--
../rh

Apr 27 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
5
by: titan0111 | last post by:
#include<iostream> #include<iomanip> #include<cstring> #include<fstream> using namespace std; class snowfall { private: int ft;
1
by: Matthew Wilson | last post by:
I need to write a function crc(msg, len) that gets a char array of length len and then calculates the crc32 for the code. I don't understand what's going wrong in the code I have. It goes...
42
by: gt8887b | last post by:
Hello! In his recent newsletter embedded expert Jack Ganssle says that programming students, as well as professional developers should readh "great code" (hight quality/well-crafted code that...
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
6
by: amrhi | last post by:
Whats wrong with my code , i cant access to next page. Looks like the text field cant be read by login.php Thanks <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
16
by: Stef Mientki | last post by:
This might be a very weird construction, but it's the most easy way in translating another language into Python (for simulation). Although it works, I like to know if this a valid construction: ...
5
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just...
2
by: Chedda | last post by:
everytime i try to compile and run the program, on the compiler it states on line where it says ofstream outfile("c:\\Users\\Michael\\Desktop\\Statistics.txt");#include <iostream> that theres an...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.