Determine the frequency of duplicate elements by analyzing an array of both numbers and strings

I need to write a function that takes an array element and a different array string as parameters, converts them into strings, and then counts the number of duplicate elements.

function count5numbers1(arr, arr1) {
  let m1 = arr.toString().match(/[5]/gi);
  let m2 = arr1.toString().match(/[5]/gi);
  if (m1 === null && m2 != null) {
    return m1 = 0;
  } else if (m1 != null) {
    return m1.length;
  } else if (m2 === null) {
    return m2 = "it's not a number";
  } else {
    return m2.length;
  }
}
console.log(count5numbers1([1, 2, 5, 43], [5]));
console.log(count5numbers1([1, 2, 3, 5], [5]));
console.log(count5numbers1([1, 2, 4, 2], [5]));
console.log(count5numbers1([2, 4, 54, 15], [5]));
console.log(count5numbers1([1, 5, 55, 555], [5]));
console.log(count5numbers1([6, 3, 2, 1], [5]));
console.log(count5numbers1(['notnumber,its a string'], []));

It is important that I find a way to achieve the same result using just a single parameter instead of two like 'm1'.

The expected output is: 1 1 0 2 6 0 it's not a number

Answer №1

function checkFor5s(arr) {
  const match1 = arr.toString().match(/[5]/g);
  if (arr.some(element => typeof element != 'number')) {
    return "There are non-numeric elements in the array";
  } else if (match1 === null) {
    return 0;
  } else {
    return match1.length;
  }
}

console.log(checkFor5s([1, 2, 5, 43]));
console.log(checkFor5s([1, 2, 3, 5]));
console.log(checkFor5s([1, 2, 4, 2]));
console.log(checkFor5s([2, 4, 54, 15]));
console.log(checkFor5s([1, 5, 55, 555]));
console.log(checkFor5s([6, 3, 2, 1]));
console.log(checkFor5s(['notnumber,its a string']));

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

Guide on how to retrieve the parent element's number when dragging starts

Within my div-containers, I have a collection of div-elements. I am looking to identify the parent number of the div-element that is currently being dragged For example, if Skyler White is being dragged, the expected output should be "0" as it is from the ...

Tips for updating or replacing items in a JavaScript array

Explaining what I need, here's the code snippet: Everything works smoothly when adding a product to the items array if it does not already exist (based on id). However, if it does exist, I only want to update the item. The distinction is based on the ...

Styling just a single div when the state changes

I have a scenario where I am rendering 4 divs based on data fetched from my backend. Each div is colored according to a state change. While I have successfully implemented this, the issue is that all 4 divs can be colored in this way simultaneously. I want ...

Extracting <p> elements from a separate HTML file and cycling through them

I successfully created a website using only HTML, JavaScript, and jQuery without any server-side scripting or language. Within my website, I have a simple stream.html page that remains unstyled with no CSS formatting. <html> <head> </head& ...

Issues with basic routing in Angular version 1 are causing problems

I'm encountering an issue while setting up my first Angular app with routing. It seems to be a simple problem but it's not working properly. Whenever I click on the links, the URL changes. index.html - file:///C:/Users/me/repos/angularRouteTes ...

Implement a jQuery slider that pauses on hovering

Currently, I am grappling with the clearInterval function in a small jQuery project. To better illustrate the issue, I have created a jsFiddle example: http://jsfiddle.net/eWTSu/ While the rotation works smoothly, I encountered an obstacle when hovering ...

Change the spread operator in JavaScript to TypeScript functions

I'm struggling to convert a piece of code from Javascript to Typescript. The main issue lies in converting the spread operator. function calculateCombinations(first, next, ...rest) { if (rest.length) { next = calculateCombinations(next, ...res ...

Unraveling the Mystery Behind Zero Array Problems

DESCRIPTION: A collection of elements is classified as zero-plentifull if it contains multiple occurrences of zeros, and each sequence of zeros consists of at least 4 items. The objective is to determine the number of zero sequences in the array if it mee ...

Emphasizing the date variable within a spreadsheet

Hey there! I'm struggling with the code below: <html> <head> <title>highlight date</title> <style> .row { background-color: Yellow; color:blue; } </style> <script type="text/javascript"> </script> &l ...

When using the `sendFile` method in Node.js Express, you will notice that the HTML content

Hey there, I'm new to nodejs and trying to create a simple website with two pages. I'm facing an issue where the content of the second file is being rendered as the first one, even though the source inspector in the browser indicates that it&apos ...

Discover the step-by-step guide to integrating pagination API in Next JS using getServerSideProps

I need to update a parameter that currently reads page=1, it should instead be set as page=${setPage} Then, when the button is pressed, it triggers a change in the table data. This is how my code looks in [table].js: How can I achieve this? Should I use ...

problem with creating a django template script

Hello, I am currently working on a code that is responsible for executing various functions within the template. I have utilized scripts to verify these functions using if-else statements and for loops. However, I am encountering some errors during this pr ...

Tips for repairing my Bookmark local-storage tool

My Bookmark Service, which stores result items with a JSON structure, is experiencing issues. It seems that the problem lies in the array not functioning properly with the local storage feature. I have attempted to use JSON.stringify on my items. va ...

transmit a variety of items via ajax (with angular)

In my project, I am gathering user inputs and storing them in an object called 'data'. This includes fields like data.username, data.password, and data.age. To send this data to the backend using Angular, I am doing the following: var submits = ...

Is there a way to dynamically change the title of a tab when new content is added to a minimized page?

Is anyone familiar with how websites like Facebook and Buzzfeed update their tab titles when there are multiple tabs open? I have noticed that sometimes they add a "(1)" or "(2)" to indicate changes on the page. Do they use JavaScript to achieve this eff ...

Confirm before closing the window

How can I get this code to function properly and show a confirmation alert after the user clicks on a button? This is essentially an "exit website button". The confirmation pop-up will have: If "OK" is clicked > the current window will close; If ...

Is there a way to utilize the reset() function within an input form?

Is there a way to automatically reset the textbox when submitting a value? Below is my code: <!DOCTYPE html> <html> <body> <ul id="myList"> </ul> <input type="text" id="myText" value="&q ...

Troubleshooting: JavaScript code not functioning properly with variable input instead of fixed value

I have encountered an issue with a JS function that I'm using. The function is shown below: // A simple array where we keep track of things that are filed. filed = []; function fileIt(thing) { // Dynamically call the file method of whatever ' ...

What could be causing checked="checked" to fail validation in JavaScript?

I'm feeling a bit puzzled about this issue. Here's the HTML code I have - <input type="radio" id="20577091" name="q_engine" value="20577091" style="position: absolute; opacity:0;" checked="checked" /> and here is the corresponding JavaScr ...

Is there a way to link the scrolling of two ag-grid data tables together for synchronized movement?

I have integrated ag-grid into my Angular project and I am looking to display two data tables (ag-grid) stacked on top of each other. Both data tables should scroll together, meaning when I scroll in table 1 or table 2, the content of both tables should m ...