Hi guys,
I'm trying to complete a function that does the following:
/* Tests if all balls selected match all balls drawn in the same order
The function takes two arguments:
an array of drawn balls
an array of selected balls.
The function code compares each ball in one array with the ball at the same position in the other array.
The function returns:
true if the arrays contain the same numbers in the same order
false otherwise.
*/
The code I have written is -
-
{
-
for (var index = 0; index < drawnBalls.length; index = index + 1)
-
{
-
if(drawnBalls[index] == selectedBalls[index])
-
{
-
return true;
-
}
-
else
-
{
-
return false;
-
}
-
}
-
-
}
-
I have two arrays drawnBalls and selectedBalls, which will both be the same length and need to check if the numbers in the arrays match in the sameorder.
Instead of returning an overall return of true or false if all the balls match, it is looping, giving me a separate result for each element. Can anyone give me some guidance on how to achieve my overall objective of, simply returning true if the values in the array are the same and in the same order, and false otherwise. Thanks in advance for any help.
17 1991
The best thing to due is use a variable in the loop. Then after the loop is done then return the variable. So the logic flows like this. First set the variable to false. Then loop through array setting variable to true if you have a match. If all elements in drawnBalls equall all elements in selectBalls. Then the variable will remain true. if any element doesn't match then set to false and break out of the for setting index to last value. This is due to the fact that if one element drawnBalls is not equal to selectBalls then the arrays are the same and you return false. Something like the following. - var match=new Boolean();
-
for (var index = 0; index < drawnBalls.length; index = index + 1)
-
{
-
if(drawnBalls[index] == selectedBalls[index])
-
{
-
match=new Boolean(true);
-
}
-
else
-
{
-
match=new Boolean (false);
-
index=drawnBalls.length;
-
}
-
}
-
-
}
-
return match.toString();
Although the idea is correct, the code could definitely be optimised. There's no need for a variable. If there is a non-match, just return false. If the loop ends and you're still in the function, that means that all values match, so return true at that point.
Thanks for the reply both,
I'm trying to amend my code to take into account both suggestions and have come up with: - var drawnBalls = [1,2,3,4,5];
-
var selectedBalls = [1,2,3,4,5];
-
-
document.write(drawnBalls + '<BR>');
-
document.write(selectedBalls + '<BR>');
-
-
function allBallsMatch(drawnBalls, selectedBalls)
-
-
{
-
for (var index = 0; index < drawnBalls.length; index = index + 1)
-
{
-
if(drawnBalls [index] == selectedBalls[index])
-
{
-
return true;
-
}
-
else
-
{
-
return false;
-
-
index=drawnBalls.length;
-
}
-
-
}
-
return true;
-
}
This still doesn't seem to be giving me the required outcome, can you please point out where I'm going wrong?
Hello boobikins,
It seems that you are returning true/false every time you compare the elements of the array thats why its not working.. instead you can use a flag variable to point out that all elements in array match or not. -
/*
-
* || JAI BHAVANI ||
-
*/
-
function allBallsMatch(drawnBalls, selectedBalls){
-
var flag=true;
-
for (var index = 0; index < drawnBalls.length; index = index + 1){
-
if(drawnBalls [index] == selectedBalls[index]){
-
flag=true;
-
}
-
else{
-
flag=false;
-
}
-
-
}
-
return flag;
-
}
i hope that, it works for you.
kind regards,
Nitin Sawant
Nitin, your code wouldn't work because, say, if it doesn't match the first one and matches the rest or even the last items, then it would return true.
I have never used flag, so how can I get my function to work returning an overall true or false? Still stuggling...
gits 5,390
Expert Mod 4TB
a flag is just a variable that holds a value for some easy comparison ... there are several ways to achieve your goal and a flag in your case would better hold a number ... acoder gave you a very good hint already ... so i'll repeat it and perhaps give you one more idea :) ... so let me write a working example in pseudocode: - var drawnBalls = [1,2,3,4,5];
-
var selectedBalls = [1,2,3,4,6];
-
-
function allBallsMatch(drawnBalls, selectedBalls) {
-
var dbl = drawnBalls.length;
-
var sbl = selectedBalls.length;
-
-
var compArray = [];
-
-
// or use a 'flag'
-
// var flag = 0;
-
-
if (dbl == sbl) {
-
for (var i = 0; i < dbl; ++i) {
-
var b = drawnBalls[i];
-
-
// in case b matches the number in selectedBalls
-
// push() something in the compArray or add 1 to
-
// your flag
-
// else break; the loop
-
}
-
}
-
-
return compArray.length == dbl;
-
-
// or in case you have used the flag
-
// return flag == dbl;
-
}
-
kind regards
@gits
would it do the work as far i understood you want that all the indexes in both the arrays should have same values/ balls wotever else it should return false
see if this helps -
function allBallsMatch(drawnBalls, selectedBalls)
-
{
-
var flg=0;
-
for (var index = 0; index < drawnBalls.length; index = index + 1)
-
{
-
if(drawnBalls [index] != selectedBalls[index])
-
{
-
flg=1;
-
}
-
-
}
-
if(flg==0){
-
return true;
-
}else{
-
return false;
-
}
-
}
-
gits 5,390
Expert Mod 4TB
have a look at post #6 ... the shown code has the same problem.
kind regards
On top of that, there's no need for any flags. You can just return false when there's definitely a non-match.
@gits
gits if this was for me then i dont think that i am returning true at any place iit would return false only if even any one of them would not match, but may be there is something wrong with it.
gits 5,390
Expert Mod 4TB
yes ... i overlooked the braces ... so it would work ... it's just bad readable ... and as acoder said it would work but could be optimized to something like this (to avoid needless memory-usage and operations): - function allBallsMatch( drawnBalls, selectedBalls ) {
-
for ( var i = 0, l = drawnBalls.length; i < l; ++i ) {
-
if ( drawnBalls[i] != selectedBalls[i] ) {
-
return false;
-
}
-
}
-
return true;
-
}
@acoder
Hello acoder,
thanks, now i got the mistake
@NitinSawant
suppose your very forst value in the array does not match and the rest all do it would set flag false for the first and then for all the rest values it would keep on setting the flag true :) isnt it , seee carefully
here is an example arrays with the elements try comparing these -
drawnBalls [0,1,2,3,4,5]
-
selectedBalls[1,1,2,3,4,5]
-
-
<!--
-
|| JAI BHAVANI ||
-
Author: Nitin Sawant
-
Email: nitinsawant@netbeans.org
-
-->
-
<html>
-
<head>
-
<script type="text/javascript">
-
var drawnBalls = new Array(1,2,3,4,5);
-
var selectedBalls = new Array(1,2,3,4,5);
-
function allBallsMatch(drawnBalls, selectedBalls){
-
var flag=true;
-
for (var index = 0; index < drawnBalls.length;index = index + 1){
-
if(drawnBalls [index] !=selectedBalls[index]){
-
return false;
-
}
-
-
}
-
return true;
-
}
-
</script>
-
</head>
-
<body>
-
<script type="text/javascript">
-
document.write("Contents of drawnBalls: <BR>");
-
for(var temp=0;temp<drawnBalls.length;temp=temp+1){
-
document.write(drawnBalls[temp]+ '<BR>');
-
}
-
document.write('<BR>');
-
document.write("Contents of selectedBalls: <BR>");
-
for(var temp1=0;temp1<selectedBalls.length;temp1=temp1+1){
-
document.write(selectedBalls[temp1]+ '<BR>');
-
}
-
-
//check if both arrays are equal
-
if(allBallsMatch(drawnBalls, selectedBalls)){
-
document.write("All balls match <BR>");
-
}else{
-
document.write("All balls doesn't match <BR>");
-
}
-
</script>
-
</body>
-
</html>
@NitinSawant
:) yeah thats it good to go
regards,
Omer Aslam
gits 5,390
Expert Mod 4TB
just some hints to optimize that: - // arrays should be instantiated with the literals
-
// for better performance -> so write this:
-
var drawnBalls = new Array(1,2,3,4,5);
-
-
// as:
-
var drawnBalls = [ 1, 2, 3, 4, 5 ];
-
-
function allBallsMatch(drawnBalls, selectedBalls){
-
// the variable flag is useless in that code so remove it
-
// var flag=true;
-
-
// it is not optimal to always evaluate the length of the array
-
// so it's better to do it just once and even the incrementation
-
// should be written shorter -> so instead of:
-
// for ( var index = 0; index < drawnBalls.length; index = index + 1 ) {
-
// use:
-
for ( var index = 0, l = drawnBalls.length; index < l; ++index ) {
-
if ( drawnBalls[index] != selectedBalls[index] ) {
-
return false;
-
}
-
}
-
return true;
-
}
-
kind regards
Sign in to post your reply or Sign up for a free account.
Similar topics
by: da Vinci |
last post by:
OK, this has got to be a simple one and yet I cannot find the answer
in my textbook.
How can I get a simple pause after an output line, that simply waits
for any key to be pressed to move on?
...
|
by: KK |
last post by:
Hi, im working on this bigInt class. Need help writing algorithm for
the operator*, andy help will be appreciated. Thanks in advance
bigInt.h...
|
by: StumpY |
last post by:
HI,
I have set up a page with a form which appends data to a .csv file on my asp
server, this is to enable a limited number of users to add news threads to
this file, which contains the data in...
|
by: Jim Hubbard |
last post by:
I want to implement the same type of activeX restrictions in my browser
application that the new SP2 for XP places in Internet Explorer.
I have found 2 web pages dealing with this functionality...
|
by: Dr. Ann Huxtable |
last post by:
Hello All,
I am reading a CSV (comma seperated value) file into a 2D array. I want
to be able to sort multiple columns (ala Excel), so I know for starters,
I cant be using the array, I need...
|
by: el prinCipante |
last post by:
I'm getting tired of the following error message.
Compiler Error message : Error: Need explicit cast to convert
from: float
to: float *
I am trying to use a routine from the Numerical Recipes...
|
by: SStory |
last post by:
Hi all,
I really needed to get the icons associated with each file that I want to
show in a listview.
I used the follow modified code sniplets found on the internet.
I have left in...
|
by: Aaron |
last post by:
Complete code follows.
I am new to .NET programming (and programming in general) and I am having a
difficult time understanding how to fill a variable in one sub, and then
access it from...
|
by: ATS |
last post by:
I'm trying to set up a slide show on a web page using Javascript. Here is
the code I have so far:
<script language="javascript">
alert("**in test area 1");
slides = new Array();
slides =...
|
by: meenasamy |
last post by:
Hi all, i need to create a function that takes three parameters(
Original currency, needed currency, amount) i need to convert from the
original currency to the needed currency the amount and...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
| |