js code

<!DOCTYPE html>
<html>
<body>



<script>

function delta(a)
{
var c = new Array();

var i ;

for( i = 0; i <a.length; i ++)
{
c[i]+=a;
return c;
}
document.write(c);
}
var hunter = delta(6621);



</script>

</body>

</html>

when I run the script nothing happened . I am stuck and I do not know why nothing happened . What is my error??
Last edited on
You have an unconditional return statement in a for loop.
I changed the unconditional return statement to a conditional return statement but it still does not work

<script>

function delta(a)
{
var c = new Array();

var i ;

for( i = 0; i <a.length; i ++)
{
if(c[i]+=a)
{
break;
}
return c;
}
document.write(c);
}
var hunter = delta(6621);

</script>
Last edited on
I changed the unconditional return statement to a conditional return statement but it still does not work


No you didn't. return c; is still unconditional and inside the loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function delta(a)
{
        var c = new Array(); //creates an array with 1 element. element is undefined
        
        var i;

        for( i = 0; i < a.length; i++) // a could be an array, but as passed in line 17, is an integer. This will not work
        {
                if(c[i]+=a) // c[i]+=a does not evaluate to a boolean expression. Will never be true. Also, each member of c that is added will be of type NaN (not a number)
                {
                        break; // will never execute
                }
                return c; // return statement still inside for-loop, still unconditional. Function returns on first iteration of loop
        }
        document.write(c); // will never execute
}
var hunter = delta(6621); //given line 7, should be an array. is integer. Conversely, line 7 could be rewritten as: for(i=0; i < a; i++); 

Also, go here http://ideone.com/. In the language menu select Javascript. It probably will not make any difference whether you use Javascript Rhino or Javascript Spidermonkey.
Last edited on
Thank you Cheraphy


1)Can you please tell me what is the meaning of "unconditional return"?

2)why could not be a value returned when the return is inside a for loop that is inside a function like in our example??

3)in my script , how to pass the "a" as an array to the function delta whenever we change the value of "a" in var hunter = delta()???




Last edited on
1) return statement not encapsulated in a conditional block, or otherwise guaranteed to always evaluate

2) I don't understand the question.

3) Declare and initialize an array, then pass that array to delta when assigning it to hunter. Or you could pass it an array literal.
1) return statement not encapsulated in a conditional block, or otherwise guaranteed to always evaluate


so by conditional return do you mean this??? :

for(i=0 ; i<4 ; i++)

{

if(a==b)

{

return d;

}

}
and by unconditional return do you mean this?? :

for(i=0 ; i<4 ; i++)

{

return d;

}






with integers passed to a function like for example :


<script>

function delta(a)
{



for( i = 0; i <2; i ++)
{

return a;

}
document.write(a);
}
var hunter = delta(80);

document.write(hunter);



</script>


the output is 80 . Each time I change the integer in "var hunter = delta(80);"

the for loop , loop through my integer then the latter is returned and output



All what I need is to change the integer in "var hunter = delta(80);" then the integer is

automatically output . I no need to declare each

time the variable a . If we consider the script above , how to do exactly the

same thing but with arrays instead of integers??


Last edited on
hi everybody

I see that my last post was reported . Was it against the rules ? I was only asking some questions to get help nothing more nothing less . So I do not understand why it was reported !!! therefore , I would like to know why my last post was reported . What was wrong with my last post?


thanks and best regards
Well. We've told you multiple times that you should find a Javascript forum. While some of us don't mind fielding questions for other languages, this is still a C++ forum.
ok thank you very much :)
Topic archived. No new replies allowed.