let y = 0;
function
setup() {
createCanvas(550, 500);
textSize(16);
text(
"Click the button to create a new "
+
"dictionary or check if the keys exist"
,
20, 20);
text(
"Key:"
, 20, 260);
key_input = createInput(
'k0'
);
key_input.position(70, 250);
key_input.size(40);
createBtn = createButton(
"Create dictionary"
);
createBtn.position(30, 40);
createBtn.mouseClicked(createNewDict);
checkBtn = createButton(
"Check if the key exists"
);
checkBtn.position(30, 290);
checkBtn.mouseClicked(checkVal);
}
function
createNewDict() {
clear();
let obj = {};
for
(let i = 0; i < 5; i++) {
let rk =
"k"
+ i;
let rn =
"v"
+ i;
obj[rk] = rn;
text(
"Key: "
+ rk +
" Value: "
+
rn, 40, 120 + 20 * i);
}
numDict = createStringDict(obj);
text(
"New Dictionary created with values"
,
20, 80);
text(
"Click the button to create a new "
+
"dictionary or check if the keys exist"
,
20, 20);
text(
"Key:"
, 20, 260);
}
function
checkVal() {
let keyToCheck = key_input.value();
let hasEntry = numDict.hasKey(keyToCheck);
if
(hasEntry) {
let keyVal = numDict.get(keyToCheck);
text(
"The value at key: "
+ keyToCheck +
" is: "
+ keyVal, 20, 340 + y * 20);
}
else
{
text(
"The key does not exist"
,
20, 340 + y * 20);
}
y++;
text(
"Click the button to create a new "
+
"dictionary or check if the keys exist"
,
20, 20);
text(
"Key:"
, 20, 260);
}