let y = 0;
function
setup() {
createCanvas(550, 500);
textSize(16);
text(
"Click the buttons to create a "
+
"dictionary or get the value of the key"
,
20, 20);
text(
"Key:"
, 20, 260);
key_input = createInput(
'user'
);
key_input.position(70, 250);
key_input.size(80);
setBtn =
createButton(
"Create dictionary"
);
setBtn.position(30, 40);
setBtn.mouseClicked(createNewDict);
getBtn =
createButton(
"Get the value of the given key"
);
getBtn.position(30, 290);
getBtn.mouseClicked(getVal);
}
function
createNewDict() {
clear();
let obj = {};
for
(let i = 0; i < 5; i++) {
let rk = ceil(Math.random() * 100);
let rn = floor(Math.random() * 100);
let rkey =
"user"
+ rk;
let rval =
"data"
+ rn;
obj[rkey] = rval;
text(
"Key: "
+ rkey +
" Value: "
+
rval, 40, 120 + 20 * i);
}
numDict = createStringDict(obj);
text(
"New Dictionary created with values"
,
20, 80);
text(
"Click the buttons to create a "
+
"dictionary or get the value of the key"
,
20, 20);
text(
"Key:"
, 20, 260);
}
function
getVal() {
let keyToCheck = key_input.value();
let keyVal = numDict.get(keyToCheck);
text(
"The value at key: "
+ keyToCheck +
" is: "
+ keyVal, 20, 340 + y * 20);
y++;
text(
"Click the buttons to create a "
+
"dictionary or get the value of the key"
,
20, 20);
}