let y = 0;
function
setup() {
createCanvas(550, 500);
textSize(16);
text(
"Click on the button to create a new "
+
"dictionary and divide the given value"
,
20, 20);
text(
"Key:"
, 20, 260);
text(
"Value:"
, 160, 260);
key_input = createInput();
key_input.position(70, 250);
key_input.size(40);
val_input = createInput();
val_input.position(220, 250);
val_input.size(40);
setBtn =
createButton(
"Create random dictionary"
);
setBtn.position(30, 40);
setBtn.mouseClicked(createNewDict);
divBtn =
createButton(
"Divide given value at key"
);
divBtn.position(30, 300);
divBtn.mouseClicked(divVal);
}
function
createNewDict() {
clear();
let obj = {};
for
(let i = 0; i < 6; i++) {
let rk = ceil(Math.random() * 100);
let rn = floor(Math.random() * 100);
obj[rk] = rn;
text(
"Key: "
+ rk +
" : Value: "
+
rn, 40, 120 + 20 * i);
}
numDict = createNumberDict(obj);
text(
"New Dictionary created with values"
,
20, 80);
text(
"Click on the button to create a new "
+
"dictionary and divide the given value"
,
20, 20);
text(
"Key:"
, 20, 260);
text(
"Value:"
, 160, 260);
}
function
divVal() {
let keyToChange = int(key_input.value());
let valToDiv = int(val_input.value());
let prevVal = numDict.get(keyToChange);
if
(prevVal) {
numDict.div(keyToChange, valToDiv);
let updatedVal = numDict.get(keyToChange);
text(
"The value at key: "
+ keyToChange +
" was: "
+ prevVal, 20, 360 + y * 40);
text(
"The updated value at key: "
+
keyToChange +
" is: "
+ updatedVal,
20, 380 + y * 40);
}
else
{
text(
"Please enter a proper key"
,
20, 380 + y * 40);
}
y = y + 1;
text(
"Click on the button to create a new "
+
"dictionary and divide the given value"
,
20, 20);
}