Question

I recently switched from regular JavaScript prompts to the jQuery Plugin 'Alertify' prompts. However, when I define a variable as the prompt's response, and use the variable in a later alert, it prints '[object Object]' instead. After a few hours of Googling and testing, I still haven't come up with an answer.

This is my code:

do {
    var fullLoop = false;
    var user = alertify.prompt("What is your name?", function (e, str) {
        if (e) {
            user.toUpperCase();
            if (str.length === 0) {
                alertify.alert("Please enter your name.");
                fullLoop = true;
            }
        } else {
            alertify.alert("Please enter your name.");
            fullLoop = true;
        }
    }, "Name");
    do {
        var bestLoop = false;
        var best = alertify.prompt("So " + user + ", what is your greatest ability, STRENGTH, SPEED, or SMARTS?", function (e, str) {
        if (e) {
            best.toUpperCase();
            if (best === "STRENGTH" || best === "SPEED" || best === "SMARTS") {
            var offset = options.indexOf(best);
            if (offset != -1) {
                options.splice(offset, 1);
            }
        } else {
            alert("Please choose either STRENGTH, SPEED, or SMARTS as your greatest ability.");
            bestLoop = true;
        }
        } else {
            alertify.alert("Please choose either STRENGTH, SPEED, or SMARTS as your greatest ability.");
            bestLoop = true;
        }
    }, "Greatest Ability");
    } while (bestLoop);
    alertify.alert("Great! So " + best + " is yor greatest ability.");
} while (fullLoop);

You can see the error I'm experiencing here.

I've also noticed that my if statements regarding the strings are ignored, I'm assuming there's a connection, but what is it? I haven't found any bugs, so am I using Alertify incorrectly?

Was it helpful?

Solution

Your usage of the alertify plugin is wrong, it the prompt method is an asynchronous method and all the actions using that have to be done in the callback.

A minified version is as follows

var options = ["STRENGTH", "SPEED", "SMARTS"];
alertify.alert("Before we begin our journey, let's learn a little bit  about you.");

alertify.prompt("What is your name?", function (e, str) {
    if (e) {
        var  user = str.toUpperCase();

        alertify.prompt("So " + user + ", what is your greatest ability, STRENGTH, SPEED, or SMARTS?", function (e, str) {
            if (e) {
                var best = str.toUpperCase();
                if (best === "STRENGTH" || best === "SPEED" || best === "SMARTS") {
                    var offset = options.indexOf(best);
                    if (offset != -1) {
                        options.splice(offset, 1);
                    }
                    alertify.alert("Great! So " + best + " is yor greatest ability.");        
                } else {
                }
            } else {
            }
        }, "Greatest Ability");
    } else {
    }
}, "Name");

Demo: Fiddle

OTHER TIPS

I don't know much about alterify, but it looks like you're trying to display a JSON object. Try JSON.stringify(user) and see what you get. It might be something like

{ "Name": "Paul" }

If so you'll then want to do

alertify.prompt("So " + user.Name + ", what is your greatest ability, STRENGTH, SPEED, or SMARTS?"

or maybe

alertify.prompt("So " + JSON.stringify(user.Name) + ", what is your greatest ability, STRENGTH, SPEED, or SMARTS?"

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top