-1

I'm trying to do a calculation with time and need to remove the ":" which splits hours and minutes.

My array currently holds a string value of "12:04"

I created a for loop to iterate through the second array string by length, check for a :, then remove that character and log the new output. However, my logic is not working as intended. If you can, please let me know what I did wrong so I can fix my issue.

    for (let i = 0; i < content[2].length; i++) {
        if (content[2].charAt(i) === ":"){
            content[2].slice(i);
            console.log(content[2])
        }
    }
Matt
  • 19
  • 3

2 Answers2

1

If you are sure that ":" will appear only once, then keep it simple

content[2] = content[2].replace(":", "");

Full code:

const result = content.map(str => str.replace(":", ""))
Abhisek Mishra
  • 269
  • 3
  • 15
  • Just curious. If the time has an AM or PM attached, how would I go about removing this? Would I need to do an iteration, or is there a method to check for string similarities? (can't seem to find) – Matt Feb 02 '23 at 05:37
  • @Matt if you want to remove everything except numbers, just do `str.replace(/\D+/g, '')` – adiga Feb 02 '23 at 05:43
  • `var parts = content[0].match(/\d+/g)` – dandavis Feb 02 '23 at 05:45
0

I think this works well:

content.split(':').join('')

Here is the output I got from the console:

 "12:04".split(':').join('')
'1204' // Output
Stanley Ulili
  • 702
  • 5
  • 7