Thanks for the help. Btw it is not a part of the course, it just a task assigned to us with certain time duration!
After examining the links still I am stuck with this part, actually getting undefined instead of specified answer.
$(document).ready(function () {
$("#runBtn").click(function () {
runcode();
});
$("#resetBtn").click(function () {
reset();
});
});
//statement block
Blockly.Blocks['Bot'] = {
init: function() {
this.appendStatementInput("NAME")
.setCheck(null)
.appendField("Bot");
this.setPreviousStatement(true, null);
this.setColour(230);
this.setTooltip("");
this.setHelpUrl("");
}
};
//dropdown block
Blockly.Blocks['Ask_me_a_question'] = {
init: function() {
this.appendDummyInput()
.appendField("Ask me a question?")
.appendField(new Blockly.FieldDropdown([["What is the date today?","Date"], ["What is the time now?","Time"], ["How are you?","Feel"], ["What is javascript?","JavaScript"], ["What is your name?","Name"]]), "OPTIONS");
this.setPreviousStatement(true, null);
this.setColour(290);
this.setTooltip("");
this.setHelpUrl("");
}
};
// Blockly.JavaScript["Bot"] = function (block) {
// var text_input = block.getFieldValue("OPTIONS");
// var code = `
// var inputTextValue = "${text_input}";
// `;
// return code;
// };
Blockly.JavaScript['Ask_me_a_question'] = function(block) {
return block.getFieldValue('OPTIONS')
};
Blockly.JavaScript['Bot'] = function(block) {
//var statements_bot = Blockly.JavaScript.statementToCode(block, 'NAME');
var dropdown_ask_me_a_question = Blockly.JavaScript.statementToCode(block, 'OPTIONS');
//console.log(dropdown_ask_me_a_question);
var question;
switch(dropdown_ask_me_a_question) {
case "Date":
var today = new Date();
question = today.getDate()+'/'+(today.getMonth()+1)+'/'+today.getFullYear();
break;
case "Time":
var today = new Date();
question = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds() + " IST";
break;
case "Feel":
question = "I am good, How are you?";
break;
case "JavaScript":
question = "JavaScript is a text-based programming language used both on the client-side and server-side that allows you to make web pages interactive";
break;
case "Name":
question = "I am a bot created by Suchitra using blockly";
break;
}
var code = `
var inputTextValue = "${question}";
`;
return code;
};
var workspace = Blockly.inject("blocklyDiv", {
media: "assets/media/",
toolbox: document.getElementById("toolbox"),
});
function redrawUi() {
if (typeof inputTextValue !== "undefined") {
$("#inputBox").text(inputTextValue);
} else {
$("#inputBox").text("");
}
}
function runcode() {
// Generate JavaScript code and run it.
var geval = eval;
try {
geval(Blockly.JavaScript.workspaceToCode(workspace));
} catch (e) {
console.error(e);
}
redrawUi();
}
function reset() {
delete inputTextValue;
redrawUi();
}