Please Help me to understand this!
function validateFields() {
"user strict";
var nameserverone = document.getElementById("ns1").value;
|var nameservertwo = document.getElementById("ns2").value;
if(nameserverone === null) {
console.log("say Hi");
return nameserverone;
}
else {
console.log("Say Bye");
}
}
and this:
function validateFields() {
"user strict";
nameserverone = document.getElementById("ns1").value;
nameservertwo = document.getElementById("ns2").value;
if(nameserverone === null) {
console.log("say Hi");
return nameserverone;
}
else {
console.log("Say Bye");
}
}
You have a syntax error.
You want it to say, "use strict" not "user strict".
You also want to put "use strict" at the top of your file.
I've attached what I think you should change your code to.
"use strict";
function validateFields() {
var nameserverone = document.getElementById("ns1").value;
var nameservertwo = document.getElementById("ns2").value;
if (nameserverone === null) {
console.log("say Hi");
return nameserverone;
}
else {
console.log("Say Bye");
}
}
W3 Schools for more details
Your function's logic doesn't make sense as it is, but maybe because it's a sample. I'll first guess what you are trying to achieve, then show you a possible solution.
First, regarding the name of your function
validateFields, I suppose you need to check if entries (ns1andns2in your example) contain valid data.So, you might want your function to return a boolean (
trueorfalse) if the fields are erroneous.Your current function doesn't check the 2 entries, and there is only a return when
nameserveroneis null (which doesn't make sense, or maybe it's what you want)."use strict"; function validateFields() { var nameserverone = document.getElementById("ns1").value; var nameservertwo = document.getElementById("ns2").value; if(nameserverone === null || nameserverthow === null) { // one of the entry is empty, we return false console.log("one of the servers' name is null"); return false; } else { // both entries contain data, should be Ok return true; } }I would recommend to try having only ONE return in your functions no matter what, such fix would do:
"use strict"; function validateFields() { var nameserverone = document.getElementById("ns1").value; var nameservertwo = document.getElementById("ns2").value; var status; if(nameserverone === null || nameservertwo === null) { // one of the entry is empty, we return false console.log("one of the servers' name is null"); status = false; } else { // both entries contain data, should be Ok status = true; } return status; }