Created
June 8, 2022 10:05
-
-
Save ackuser/fbe95bed8af419cbca7bed4c73421527 to your computer and use it in GitHub Desktop.
ArrayRemovingElementByReplacingLast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SPDX-License-Identifier:UNLICENSED | |
pragma solidity ^0.8.7; | |
contract Messagebox { | |
string[] public messages; | |
uint public lastDate; | |
address public owner; | |
constructor () { | |
messages.push("HELLO WORLD!"); | |
owner = msg.sender; | |
} | |
function setMessage(string memory _new_message) public payable { | |
require (msg.value <= 10); | |
messages.push(_new_message); | |
lastDate = block.timestamp; | |
} | |
function getLength() public view returns (uint) { | |
return messages.length; | |
} | |
function deleteMessage(string memory message) public returns (string[] memory) { | |
require(msg.sender == owner, "Ownable: You are not the owner, Bye."); | |
require(messages.length > 0, "Empty array, Bye."); | |
if(messages.length == 1) { | |
messages.pop(); | |
}else{ | |
for(uint i = 0; i < messages.length; i++){ | |
if(keccak256(bytes(messages[i])) == keccak256(bytes(message))) { | |
messages[i] = messages[messages.length - 1]; | |
messages.pop(); | |
} | |
} | |
} | |
return messages; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment