Code Assistance Showdown: Google Gemini vs. ChatGPT 3.5
Jeevachaithanyan Sivanandan
[ Python + PHP + JavaScript ] - Software Engineer / Frontend Developer / Full Stack Developer
I inquired of both Google Gemini and ChatGPT3.5 with the identical question provided below:
I require JavaScript code to extract content up to, but not including, the <hr> tag from the below REST api response
"content": {
"rendered": "\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse volutpat est at eros vehicula vulputate. Nullam pulvinar iaculis diam, quis sagittis enim auctor sit amet. Suspendisse molestie maximus ante, at dictum nulla lacinia ac. Integer lacus ex, pellentesque in enim vel, cursus egestas elit. Proin in varius nibh.<\/p>\n\n\n\n<hr class=\"wp-block-coblocks-dynamic-separator\" style=\"height:50px\"\/>\n\n\n\n<figure class=\"wp-block-coblocks-gallery-masonry masonry-grid has-lightbox has-medium-gutter\">\n
}
领英推荐
The response was provided by ChatGPT3.5.
// Sample response from WordPress API
const response = {
"content": {
"rendered": "\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse volutpat est at eros vehicula vulputate. Nullam pulvinar iaculis diam, quis sagittis enim auctor sit amet. Suspendisse molestie maximus ante, at dictum nulla lacinia ac. Integer lacus ex, pellentesque in enim vel, cursus egestas elit. Proin in varius nibh.<\/p>\n\n\n\n<hr class=\"wp-block-coblocks-dynamic-separator\" style=\"height:50px\"\/>\n\n\n\n<figure class=\"wp-block-coblocks-gallery-masonry masonry-grid has-lightbox has-medium-gutter\">\n"
}
};
// Extract content before the <hr> tag
const renderedContent = response.content.rendered;
const extractedContent = renderedContent.split('<hr')[0];
console.log(extractedContent.trim()); // Output the extracted content
for the same question google Gemini gave me below answer
function extractContentBeforeHr(htmlString) {
// Create a DOM parser to handle HTML parsing safely
const parser = new DOMParser();
const doc = parser.parseFromString(htmlString, 'text/html');
// Find all HR elements and return the closest preceding sibling's text content
const hrElements = doc.querySelectorAll('hr.wp-block-coblocks-dynamic-separator');
if (hrElements.length > 0) {
const hrElement = hrElements[0];
const precedingSibling = hrElement.previousElementSibling;
// Check if a preceding sibling exists to avoid potential errors
if (precedingSibling) {
// Use textContent to get the combined text content of all child nodes,
// including text and other elements within the desired content area
return precedingSibling.textContent;
} else {
return ''; // Return an empty string if no preceding sibling is found
}
} else {
// Return the original string if no HR element is found
return htmlString;
}
}
// Usage example:
const htmlResponse = {
"content": {
"rendered": "\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse volutpat est at eros vehicula vulputate. Nullam pulvinar iaculis diam, quis sagittis enim auctor sit amet. Suspendisse molestie maximus ante, at dictum nulla lacinia ac. Integer lacus ex, pellentesque in enim vel, cursus egestas elit. Proin in varius nibh.<\/p>\n\n\n\n<hr class=\"wp-block-coblocks-dynamic-separator\" style=\"height:50px\"\/>\n\n\n\n<figure class=\"wp-block-coblocks-gallery-masonry masonry-grid has-lightbox has-medium-gutter\">\n"
}
};
const extractedContent = extractContentBeforeHr(htmlResponse.content.rendered);
console.log(extractedContent);
I find that Google Gemini often complicates simple solutions, particularly in coding-related queries. In contrast, ChatGPT excels in providing straightforward and effective responses. ChatGPT demonstrates a better grasp of contextual understanding in questions, whereas Gemini relies solely on tokens without such comprehension. However, one advantage of Google Bard is its access to the Google search engine, ensuring up-to-date information.