-1

this code it checks to see if the input boxes are empty, but does this return with or without it work? and this variable with true after its value is changed it continues with same value for next execution, its value should not be maintained until I send a new value?

CODE:

<input value="g" type="text" placeholder="a" class="test">
<input value="" type="text" placeholder="a" class="test">

<button onclick="win()">okkk</button>


var inputs = document.getElementsByClassName('test');
var len = inputs.length;
var valid = true;

for(var i=0; i < len; i++) {
    if (!inputs[i].value) {
        valid = false;
    }
}

if (valid) {
    console.log("ok");
    return true;
} else {
    console.log("no");
    return false;
}
Nest
  • 3
  • 1
  • 5
  • 1
    Please post the code – Sundeep Pidugu Apr 21 '19 at 20:16
  • @Nest: No he is correct :) Next time, please try to post a complete answer in one go. I guess you got the downvote because your question isnt complete :) – Martijn Apr 21 '19 at 20:22
  • @Martijn: Sorry, the next time I try to explain it better. but friend this "return" without it the code still works so if I want I can withdraw that nothing will happen? – Nest Apr 21 '19 at 20:26

2 Answers2

0

Wether the function has to return can not be decided by this snippet. If this is the code of a function, it completely depends on how that function is used.

If you have something like the following example, the function must return a value for this to work:

if( isMyFormValid() ){ /* ... */}

Based on the fact that your function does nothing else (eg: it doesn't change a color, a database, a warning, whatever) it seems like it just tests wether or not the checked elements are valid and the outcome must be returned to do something with it.


You dont need to save the value of valid, but you can if you want to. Declare a var outside of the function and just set the value via the function.
The reason you dont need to, is because you probally dont care to know 100% of the time, just when submitting. So in the onSubmit you do isMyStuffValid(), if not -> alert. If so continue. Only when they press again you need to check wether it's valid this time.

I recommend you read this about scopes. It explains when/where a variable exists.

Martijn
  • 15,791
  • 4
  • 36
  • 68
  • I take that doubt, but on the variable it inside the if gets value of false after the function ta is correct it gives the value of true being that the variable was previously stored as false understands? – Nest Apr 21 '19 at 20:44
  • Not really sure what you're asking, but I think you're looking for a concept called Scope. I've add that to my answer – Martijn Apr 21 '19 at 20:45
0

A function doesn't have to return a value if this is what you asked. Also, the last part of your code can be

...
console.log(valid ? 'ok' : 'no');
return valid;
kwrush
  • 171
  • 1
  • 2
  • I know, but on the variable it inside the if gets false value after the correct ta function it gives the value of true being that the variable was previously stored as false understands? – Nest Apr 21 '19 at 20:40
  • Sorry I don't understand your question. This stackoverflow post might be helpful https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – kwrush Apr 21 '19 at 20:54