Answer input - AppLab

My class are currently using AppLab to create a range of different apps and one of them is trying to use a text input button for a user to type in an answer for the app to then change screens to show whether the answer is correct or not. He has worked out how to insert a text input box, but how would he build the code to assess the answer as correct or incorrect? Thanks in advance!

Here’s the link to his project: https://studio.code.org/projects/applab/GtGj8C53EzwTNUeCNDc0ONThrVBKHqQL2KVitKdrbSM/view

I was unable to access the code. It does not have share permissions. But the student would need a “change” event for the textbox. Get the text typed in by the user by using getText and then compare the text to the answer using “==”. Here is an example:

onEvent(“user_guess”, “change”, function(event) {
//get user input and convert to lowercase
var currentGuess = getText(“user_guess”).toLowerCase();

//Display error message if blank entered or nothing entered
if (currentGuess == " " || currentGuess == “”){
showElement(“blankErrorLabel”);
setText(“user_guess”, “”);
}
else {
if (currentGuess == value)
setScreen(“winScreen”);
else
setScreen(“loseScreen”);
}
});

I am looking for a similar answer, but am wondering if I can have input entered by a student calculated to verify it is equal to a number?

I am trying to create a math bowling app where they are given three random numbers and they need to create math problems using those 3 numbers to get the numbers 1, 2,3…10.

I have added a picture of what I would like the students to be able to type and then I would like to have the app calculate what they type to see if it equals 1,2,3…10. The numbers are random each time so I would need a way to calculate what they are typing in the input.

Thank you for your help.

Hey @ddonnell

For your solution, one of the easiest things would be to use eval() to run code that is inputted. It will say it is harmful, however unless your app collects data using records or key values it is perfectly safe.

Here’s a simple example. Not the best looking code, but it should work:
var num1; // First number that is needed
var num2; // Second number that is needed
var num3; // Third number that is needed

onEvent("button1","click",function(){
for(var i=1;i<10;i++){
var targetInput=getText("text_input"+i);
if(eval(targetInput)==i && targetInput.includes(num1.toString()) && targetInput.includes(num2.toString()) && targetInput.includes(num3.toString())){
// Code if true
}else{
// Code if false
}
}
});

Please keep in mind that you might have to change the variables slightly to work with your project (ex. if one of your inputs had the id “input1”, you would change targetInput to equal getText(“input”+i) )

Hope this helps!

2 Likes

Hi @ddonnell ,

Welcome to the code.org forum! To my understanding, there’s no simple way to do this, at least not reasonably within the scope of the CS Principles curriculum. I’m sure there’s some way to get it done, but I’ll let someone more knowledgeable about programming answer that.

I feel one way would be to basically parse the input (if that’s the right word) and basically use IF statements to decide what operation to perform on the numbers… but it’d be horribly tedious and the allowance of parenthesis complicates things.

I also imagine there might be some way to have the compiler (not sure if that’s the right term… the thingy that runs the JavaScript) to interpret those strings as code (and seeing the response from @letti42 , that looks to be the case) - maybe it just does mathematical evaluations? Otherwise I can see the potential danger in allowing the user to type code the interpreter can run directly (basically allowing the user to “inject” harmful code into your program), but if it’s limited to just mathematical stuff then maybe worst case the user types something that doesn’t make sense mathematically (aka text) and the program crashes.

That is definitely a possibility. I guess you could have a “checker” of some sort that only allows for certain characters such as 0-9, +, -, *, /, etc. with the help of an array and for loop cooperation. This way any code unrelating to maths equations would be disallowed for eval.