Function return value undefined [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have this function:
function addressVerification() {
var del;
let addVer = $('#add_verified').val();
let maddVer = $('#madd_verified').val();
if (addVer == "yes" && maddVer == "yes") {
del = true;
} else {
del = false;
}
return del;
}
When I call it:
$('#add_verified').change(function(){
var ver = addressVerification();
console.info("This is the returned value from the verified address function: "+ ver);
});
It comes back as "undefined."
What am I doing wrong?

You have a syntax error
function addressVerification() {
var del;
let addVer = $('#add_verified').val();
let maddVer = $('#madd_verified').val();
if (addVer == "yes" && maddVer == "yes") {
del = true;
} else {
del = false;
}
} . <----- This is closing the function, ant thus, returning undefined
return del;
}

Related

getting NaN when asking for video duration [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Can anyone help me understand why console.log(this.getPlayerDuration()) inside of setAttendancePromptTimeLimit() is returning NaN?
// video-player.js
const videoP = document.querySelector('#in-course-player')
class videoPlyer {
constructor(videoElem){
this.vidPlayer = videoElem
}
play = function(cb){
arguments.length ? cb : console.log('play')
}
addListener = (elem, event, cb) => {
elem.addEventListener(event, cb)
}
getPlayerCurrentTime = () => this.vidPlayer.currentTime
getPlayerDuration = () => this.vidPlayer.duration
showVideoDuration = function(cb){
arguments.length ? cb : this.addListener(this.vidPlayer, 'loadedmetadata', () =>
console.log(this.getPlayerDuration()))
}
setAttendancePromptTimeLimit = cb => {
// returns NAN
console.log(this.getPlayerDuration())
}
init = () => {
//returns 16.1
this.showVideoDuration()
//returns NAN
this.setAttendancePromptTimeLimit()
}
}
const coursePlayer = new videoPlyer(videoP)
coursePlayer.init()
[...] If no media data is available, the value NaN is returned. [...]
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/duration

What is the correct way to make validations in JS, using the IF control structure [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I want to know which control structure to handle validations is better
I tried with if else if, and if -> return... if -> return
Which is better and more understandable?
if(user.name == null) {
errorMessage ('insert name')
} else if (user.age == null) {
errorMessage('Insert age')
} else {
insertUser(user)
}
OR
if(user.name == null) {
errorMessage ('insert name')
return
}
if (user.age == null) {
errorMessage('Insert age')
return
}
insertUser(user)
These are not equivalent, so it's not a matter of which one is better style.
What will happen if name is null and age is also null if you use the first code block? Answer: you will only set the name, and the age will remain null.
So you should use the second block.
Also, instead of the check if (a == null) you should generally consider using something like if (!a). This handles not only null values but also a few more falsy values (e.g. undefined, '', etc.).
The better way would be to collect all the errors first. Then check if you have any errors and finally insert a user if no errors are found.
const errors = ['name', 'age'].forEach( field => {
if(user[field] === null) {
return `${field} is required`
}
})
if (errors.length === 0) {
insertUser(user)
} else {
showErrors(errors)
}
In terms of performance, they are the same.
Regarding code readability, I prefer the later.
For maintenance, the later, as you may want later a list of all errors and it will be easier to implement.
How about:
try {
check(user.name, 'insert name');
check(user.age, 'insert age');
...
insertUser(user);
} catch(e) {
// handle e
}
const check = (value, message) => {
if (!value) {
throw `${message}`;
}
};

j-query showing syntax error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i have a slider its not auto-play when i use this j-query code its showing syntax error. But i did'nt find syntax error. Please help
jQuery(document).ready(function () {
var photoslider = new WallopSlider('.photo-slider');
//autoplay
var count = photoslider.allItemsArrayLength;
var start = photoslider.currentItemIndex;
var end = count + 1;
var index = start;
jQuery(function () {
setInterval(function () {
photoslider.goTo(index);
++index;
if (index == end) {
index = start
}
}, 3000);
});
Please put }); after your coding
You forget to put the closing curly braces and parenthesis of jQuery(document).ready(function() {. Which is })
jQuery(document).ready(function () {
var photoslider = new WallopSlider('.photo-slider');
//autoplay
var count = photoslider.allItemsArrayLength;
var start = photoslider.currentItemIndex;
var end = count + 1;
var index = start;
jQuery(function () {
setInterval(function () {
photoslider.goTo(index);
++index;
if (index == end) {
index = start
}
}, 3000);
});
}); //--> this is missing

print a for-loop in the console log inside an if-statement [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Here is my code, and the JSFiddle.
I can't make the code work and I think it is because the if-statement that includes a for loop inside.
How can I make the whole code work, I've tried it with 4 different code possibilities and none of them worked/
var commands = {
'BOH': {
text: 'BOH!',
},
'HALO': {
text: 'HALO!',
},
'help()': {
text: 'Please press F12 and see the value list.',
},
};
$(".code").on("click", function () {
var codePrompt = prompt("enter the value"),
command = commands[codePrompt],
alertMessage = "";
consoleMessage = "Used '" + codePrompt + "' value.";
if(!command) {
alertMessage = "We are sorry but you entered a WRONG value.";
} else {
alertMessage = command.text;
};
if(command == 'help()') {
for (key in commands){
console.log(commands[key]);
alertMessage = command.text;
};
};
alert(alertMessage);
console.log(consoleMessage);
});
Shouldn't this
if(command == 'help()') {
be
if(codePrompt == 'help()') {

Node, RSVP.js array of promises [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
i can get the RSVP array of promises to work:
//if user wants to change username
var promises = [];
promises['username'] = new RSVP.Promise(function(resolve, reject){
if(user.username !== session.username){
//check if username is available
$model_users.getBy({
what:'id',
where: " username = "+db.escape(user.username) +" AND id != "+ db.escape(session.id_user)
})
.then(function(results){
if(!results || results.length <= 0){
//update username is available
$model_users.update({
update:{username: db.escape(user.username)},
where: " id = "+ db.escape(session.id_user)
})
.then(function(success){
confirm.username = 'was_saved';
resolve(success);
},function(err){
reject(err);
console.log('db-error 55299 ' + err);
res.send('db-error');
});
}else{
validation.username = 'error_username_exists';
resolve(validation);
res.send({"validation":validation});
}
},function(err){
reject(err);
console.log('db-error 0299 ' + err);
res.send('db-error');
});
}else{
reject('no username to update - is same to session username');
}
}
//create new JWT and send it back to FE localSTorage
RSVP.all(promises).then(function(){
$model_users.getBy({
what:'id , username',
where: " id = "+ db.escape(session.id_user)
}).then(function(results){
if(results && results.length > 0){
//set new user session and token
var auth = {};
auth.username = results[0].username;
auth.id_user = results[0].id;
auth.session_token = jwt.sign(auth, config.session_secret_key, { expiresInMinutes: config.session_expiration });
res.send({"auth":auth,"confirm":confirm});
}else{
res.send('db-error');
}
},function(err){
console.log('db-error 0.577 '+ err);
});
});
the error returned is:
RSVP.all(promises).then(function(){
^^^^
SyntaxError: Unexpected identifier
How is his possible? i'm following the official doc https://github.com/tildeio/rsvp.js/#arrays-of-promises
I setted it on top of node app.js :
var RSVP = require('rsvp');
You're missing a ) two lines above RSVP.all(promises)....
Consider using a linter, which will catch this sort of error immediately. See e.g. SublimeLinter.

Categories

Resources