Skip to content

Instantly share code, notes, and snippets.

@callezenwaka
Last active February 19, 2022 00:45
Show Gist options
  • Save callezenwaka/e57eff3f7cd1b5c6a1463ccb9e2b3230 to your computer and use it in GitHub Desktop.
Save callezenwaka/e57eff3f7cd1b5c6a1463ccb9e2b3230 to your computer and use it in GitHub Desktop.
Handle array in javascript
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Storage
* @dev Store & retrieve value in a variable
*/
contract Array {
uint[] public numbers;
string[] public words;
function getNumbers() public view returns(uint[] memory) {
return numbers;
}
function addNumber(uint number) public {
numbers.push(number);
}
function getNumber(uint num) public view returns(bool) {
for (uint number = 0; number < numbers.length; number++) {
if(numbers[number] == num) {
return true;
}
}
return false;
}
function getWords() public view returns(string[] memory) {
return words;
}
function addWord(string memory word) public {
words.push(word);
}
function getWord(string memory word) public view returns(bool) {
for (uint i = 0; i < words.length; i++) {
string memory tmp = words[i];
// https://ethereum.stackexchange.com/a/30914
if(keccak256(abi.encode((tmp))) == keccak256(abi.encode((word)))) {
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment