function
setup() {
createCanvas(500, 300);
textSize(16);
text(
"Click on the button to add new "
+
"values or get the highest key"
,
20, 20);
text(
"Key:"
, 20, 60);
text(
"Value:"
, 160, 60);
key_input = createInput(
'1'
);
key_input.position(70, 50);
key_input.size(40);
val_input = createInput(
'1'
);
val_input.position(220, 50);
val_input.size(40);
setBtn = createButton(
"Add new item"
);
setBtn.position(30, 100);
setBtn.mouseClicked(addtoDict);
getBtn = createButton(
"Get Highest Key"
);
getBtn.position(160, 100);
getBtn.mouseClicked(getHighestKey);
numDict = createNumberDict(100, 0);
}
function
addtoDict() {
clear();
let key = int(key_input.value());
let val = int(val_input.value());
numDict.set(key, val);
text(
"New key-value added to dictionary"
,
20, 160);
text(
"Key:"
, 20, 60);
text(
"Value:"
, 160, 60);
text(
"Click on the button to add new "
+
"values or get the highest key"
,
20, 20);
}
function
getHighestKey() {
let highestKey = numDict.maxKey();
text(
"The highest key in the dictionary is: "
+
highestKey, 20, 200);
text(
"Key:"
, 20, 60);
text(
"Value:"
, 160, 60);
text(
"Click on the button to add new "
+
"values or get the highest key"
,
20, 20);
}