Skip to content
This repository was archived by the owner on Aug 17, 2024. It is now read-only.

Conversation

batuncer
Copy link

  • Your Name: BAKI
  • Your City: LONDON
  • Your Slack Name:BAKI

server.js Outdated
app.post("/messages", (request, response) => {
let newMessage = request.body;
if (
newMessage.id === "" ||

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you trying to validate the id, from and text ? if they don't exist it will be a bad request right?

your logic check only if the parameter is an empty string, but what if the caller gives an undefined value?

server.js Outdated
// Helper function to get messages containing the search word
const getMessagesFromSearch = (messages, word) => {
return messages.filter((message) =>
message.text.toLowerCase().includes(word.toLowerCase())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another way to search in case insensitive with regular expression. with it you don't need to lower the message and the word.

// Value to search (can be obtained from user input or any other source)
const searchValue = 'a'; // Change this value as needed

// Function to search using regular expression
function searchWithRegex(array, searchValue) {
  const regex = new RegExp(searchValue, 'i'); // 'i' for case-insensitive search
  return array.filter(item => regex.test(item));
}

// Perform the search and store the result in a new array
const searchResult = searchWithRegex(arrayToSearch, searchValue);


// Helper function to get the 10 latest messages
const getLatestMessages = (messages) => {
return messages.slice(Math.max(messages.length - 10, 0));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of slice which return an shallow copy.


// Helper function to get a message by ID
const getMessageByID = (messages, id) => {
return messages.filter((message) => message.id == id);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI. Another way to store the message is to use Map(), so you can easily get item by id.
But it is not for search.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants