The String.includes method is unable to detect substrings within a string

Below is the text that I currently have:

 LOG: time="2021-12-09T16:55:25+01:00" level=debug msg="🎁  GetLatestBlock called" blockHeight=3 blockID=ee98016d268630db54b814d18d0127edac8cc36f90d193c0c6f5fd4909bbd8b1
    ,LOG: time="2021-12-09T16:55:25+01:00" level=debug msg="👤  GetAccountAtLatestBlock called" address=f8d6e0586b0a20c7
    ,LOG: time="2021-12-09T16:55:26+01:00" level=debug msg="️✉️   Transaction submitted" txID=5a46897e60f13ee68e11ef754983fefe9ec7bc8a2ca6079f0665e404138735e9
    ,LOG: time="2021-12-09T16:55:26+01:00" level=info msg="⭐  Transaction executed" computationUsed=6 txID=5a46897e60f13ee68e11ef754983fefe9ec7bc8a2ca6079f0665e404138735e9
    ,LOG: time="2021-12-09T16:55:26+01:00" level=debug msg="\x1b[1;34mLOG\x1b[0m \x1b[2m[5a4689]\x1b[0m \"Hello from Emulator\"""
    time="2021-12-09T16:55:26+01:00" level=debug msg="\x1b[1;34mLOG\x1b[0m \x1b[2m[5a4689]\x1b[0m 0x1cf0e2f2f715450"
    time="2021-12-09T16:55:26+01:00" level=debug msg="\x1b[1;34mLOG\x1b[0m \x1b[2m[5a4689]\x1b[0m 0x179b6b1cb6755e31"
    time="2021-12-09T16:55:26+01:00" level=debug msg="📦  Block #4 committed" blockHeight=4 blockID=5fd780a98baad6d30f66cf75e76c3e1c9398097a9bb2e3f239f0cd7e166f6932
    ,LOG: time="2021-12-09T16:55:26+01:00" level=debug msg="📝  GetTransactionResult called" txID=5a46897e60f13ee68e11ef754983fefe9ec7bc8a2ca6079f0665e404138735e9

The above content is saved in a variable named transactionLogs. When I output this variable using console.log(transactionLogs), it appears as a string.

I aim to verify if the transaction logs contain two specific addresses. One belonging to Alice with ID 0x01cf0e2f2f715450 and another one for Bob with ID 0x179b6b1cb6755e31.

The issue arises when I utilize the includes method like this (assuming Alice and Bob are both strings):

Alice; // 0x01cf0e2f2f715450
Bob; // 0x179b6b1cb6755e31
transactionLogs.includes(Alice); // false
transactionLogs.includes(Bob); // true

Surprisingly, searching for Alice returns false. I suspect it has something to do with escaped backslashes, hence I tried running

transactionLogs.replace(String.fromCharCode(92), '')
but it did not alter the outcome.

What is causing this behavior? Why does includes fail to return true for Alice?

Answer №1

Why isn't Alice returning a true value for includes?

This is because the specific string related to Alice is not found in the log:

const transactionLogs = `
 LOG: time="2021-12-09T16:55:25+01:00" level=debug msg="🎁  GetLatestBlock called" blockHeight=3 blockID=ee98016d268630db54b814d18d0127edac8cc36f90d193c0c6f5fd4909bbd8b1
    ,LOG: time="2021-12-09T16:55:25+01:00" level=debug msg="👤  GetAccountAtLatestBlock called" address=f8d6e0586b0a20c7
    ,LOG: time="2021-12-09T16:55:26+01:00" level=debug msg="️✉️   Transaction submitted" txID=5a46897e60f13ee68e11ef754983fefe9ec7bc8a2ca6079f0665e404138735e9
    ,LOG: time="2021-12-09T16:55:26+01:00" level=info msg="⭐  Transaction executed" computationUsed=6 txID=5a46897e60f13ee68e11ef754983fefe9ec7bc8a2ca6079f0665e404138735e9
    ,LOG: time="2021-12-09T16:55:26+01:00" level=debug msg="\x1b[1;34mLOG\x1b[0m \x1b[2m[5a4689]\x1b[0m \"Hello from Emulator\""
    time="2021-12-09T16:55:26+01:00" level=debug msg="\x1b[1;34mLOG\x1b[0m \x1b[2m[5a4689]\x1b[0m 0x1cf0e2f2f715450"
    time="2021-12-09T16:55:26+01:00" level=debug msg="\x1b[1;34mLOG\x1b[0m \x1b[2m[5a4689]\x1b[0m 0x179b6b1cb6755e31"
    time="2021-12-09T16:55:26+01:00" level=debug msg="📦  Block #4 committed" blockHeight=4 blockID=5fd780a98baad6d30f66cf75e76c3e1c9398097a9bb2e3f239f0cd7e166f6932
    ,LOG: time="2021-12-09T16:55:26+01:00" level=debug msg="📝  GetTransactionResult called" txID=5a46897e60f13ee68e11ef754983fefe9ec7bc8a2ca6079f0665e404138735e9
`;

const Alice = `0x01cf0e2f2f715450`;
const Bob = `0x179b6b1cb6755e31`;

console.log({
  Alice: !transactionLogs.includes(Alice),
  Bob: transactionLogs.includes(Bob),
});

Answer №2

It appears that the hex address you are looking for has been misprinted in the transaction log.

I suggest adjusting the transaction log logic to prevent cutting off the leading zeros when recording addresses.

If not, you will need to eliminate the leading zeros from the address you are trying to find using this code:

transactionLog.includes('0x' + parseInt(Alice, 16).toString(16))

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with jQuery click function not triggering after loading content using getjson function

After making a getJSON request to an API and receiving data back, I proceeded to iterate over the results and populate a table with the information. Adding a 'team' class to one of the 'td's, I created a click function that should toggl ...

Absolute file path reference in Node.js

I'm working on a Node.js project using WebStorm IDE. Here's the structure of my project: The root folder is named "root" and inside are 2 folders: "main" and "typings". The "main" folder has a file called "foo.ts", while the "typings" folder co ...

What is the most suitable data type to represent an empty object?

When I declared the return type of the function below as {}, eslint flagged an error stating not to use {} as a type because it actually means "any non-nullish value". After understanding the meaning behind this error, I realize that specifying return typ ...

Using data fetched from PHP in HTML via jQuery post: A step-by-step guide

I have created a function in jQuery that uses $.post to send data to a PHP file. The query in the PHP file is working fine and returning the data back successfully. JavaScript code: function send_agenda_data(cidade_data){ var data = {'cidade_dat ...

An Effective Method for Ensuring Array Strictly Ascending through Segment Modifications

I'm currently tackling a problem that requires me to calculate the minimum number of moves needed to transform an array into a strictly increasing sequence. These moves involve selecting a section of the array and adding a positive integer to all elem ...

Can we use an open-ended peer version dependency in package.json?

Question Summary Is it necessary for me to specify the peer dependency of @angular/core in my package.json file for a package containing easing functions that work with any version of Angular? "peerDependencies": { "@angular/core": "x.x" } Context I ...

Upon the initial rendering, the console log displays a null value for the data

Currently, I am utilizing the useEffect hook to populate my state during the initial rendering phase. const [data, setData ] = useState({}); const [isLoading, setIsLoading] = useState(false); useEffect(() => { fetchData(); }, []) const fetchDa ...

Discover how to access JSON data using a string key in Angular 2

Trying to loop through JSON data in angular2 can be straightforward when the data is structured like this: {fileName: "XYZ"} You can simply use let data of datas to iterate over it. But things get tricky when your JSON data keys are in string format, li ...

What is the best way to showcase the keys in an array?

Here's an example of an array: Array ( [0] => a [1] => b [3] => c [4] => d [15] => e ) I'm looking to rearrange it to look like this: Array ( [0] => a [1] => b [2] => c [3] => d [4] => e ) Any tips on h ...

What could be the reason behind the inability of the array in C to swap its final elements

There seems to be a problem with this program I'm working on. I am trying to modify an array of 6 elements using a function that is supposed to swap the elements, but it's not working as expected. For example, given the array array[6] = {1, 2, 3 ...

Significant Google Maps malfunction detected with API V3

Update: Issue resolved, check out my answer below. The scenario: User interacts with a map-image google maps API V3 is loaded using ajax the map is displayed in a dialog window or lightbox What's going on: The map loads and all features are funct ...

Creating a setup with Angular 2+ coupled with Webpack and a Nodejs

I have successfully configured Angular2+webpack along with NodeJs for the backend. Here is a basic setup overview: webpack.config.js: var webpack = require('webpack') var HtmlWebpackPlugin = require('html-webpack-plugin') var Extract ...

Is it possible for an object to be undefined in NextJS Typescript even after using a guard

Hey there, I could really use some help with a React component I'm working on. This is one of my initial projects and I've encountered an issue involving fetching async data. Below is the current state of my solution: import { Component } from &q ...

Enhance the design of MDX in Next.js with a personalized layout

For my Next.js website, I aim to incorporate MDX and TypeScript-React pages. The goal is to have MDX pages automatically rendered with a default layout (such as applied styles, headers, footers) for ease of use by non-technical users when adding new pages. ...

ERROR: The value property is undefined and cannot be read in a ReactJS component

Can someone help me with the error I'm encountering in the handleChange function? I'm not sure what the issue could be. const [testState, setTestState] = useState({ activeStep:0, firstName: '', lastName: '&apos ...

Would parallax stars be overwhelming for a webpage?

I'm a newcomer to the world of web development and I have an idea to make stars move across my website. However, I'm concerned about whether this might be too much for a simple website to handle. Is there a way to optimize the code? const canvas ...

Vue.js methods bound as properties on a parent object

There are times when I come across scenarios where it would be convenient to bind methods as an object property rather than a direct Vue method. For instance, instead of: <MyInput :formatter="currencyFormat" :parser="currencyParser& ...

Tips for redirecting in the callback of a jQuery post request

After receiving data, my homepage does not redirect to the login page. Below is the JavaScript code for the homepage: $(function(){ const submit = $('#submit'); const save = $('#save'); const email = $('#email'); ...

Is your PHP, MySQL array not displaying properly in a single HTML table despite multiple queries being made?

I found a code snippet on this website, which I utilized as shown below $data = array(); while($row = mysql_fetch_assoc($num1)) {$data['row'][] = $row;} while($row = mysql_fetch_assoc($num2)) {$data['row2'][] = $row;} $count = ...

Conditional AJAX within the append function

Utilizing an Ajax call to an API, I am retrieving a list of cars along with their specifications within a subset. This information is then displayed in a div using the append function. I have a default_spec_id value which I compare with each car's id. ...