-1

I have a JavaScript string which consists of two numbers separated by a /.

I want to be able to separately be able to get each of the numbers.

When there are only 1 digit integers, I can get the values pretty easily, but I don't know what to do when there are two digits integers.

Single Digits:

var string = "5/9";
var firstVal = string.slice(0, 1);
var secondVal = string.slice(-1);

Multiple Digits Variations:

var string = "10/23";
var string = "9/21";
var string = "45/2";
2
  • 5
    let [a, b] = '10/23'.split('/')? Commented Aug 25, 2021 at 7:37
  • 1
    just use the split('/') function on each string. Commented Aug 25, 2021 at 7:37

4 Answers 4

2

const num = "23/2/3"
const result = num.split("/")

console.log(result);

You can easily achieve that using string.split. This is known as string delimiter

Sign up to request clarification or add additional context in comments.

Comments

2

You can use split like this:

var string = "10/23";

console.log(string.split("/"))

Comments

1

Try to break your problem into steps.

Breaking the string into two parts

console.log("bob/marie".split("/"));
//  ["bob", "marie"]

Casting the strings to numbers

Let's assume that you are sure that they are valid numbers. There are many different ways to cast numbers in Javascript. My favourite is this multiply by 1.

console.log("132445.85" * 1);
// 132445.85 as number

Combine the two operations

console.log( "6532.11/4535.77".split("/").map( v => 1 * v ) );

The map above applies the function in all elements.

Finally let's put this in data in some HTML result, just for fun

function showNumbers(element) {
  // read the input value as string
  let text = element.value;
  // convert the string into an array breaking by "/"
  let numbers = text.split("/").map( v => v * 1 );
  // get the div element that we are going to put the list
  let myDiv = document.getElementById("my-div");
  // create a list
  let ul = document.createElement("ul");
  // creating a total column
  let total = 0;
  numbers.forEach(
    function(n) {
      // create a list item
      let li = document.createElement("li");
      // set the number as the list item value
      li.innerHTML = n;
      // add the list item into the list
      ul.appendChild(li);
      // we can add the numbers
      total += n;
    }
  )
  // create a list item
  let liTotal = document.createElement("li");
  // set the number as the list item value
  liTotal.innerHTML = "Total " + total;
  // add the list item into the list
  ul.appendChild(liTotal);
  // remove all elements from my div
  myDiv.innerHTML = "";
  // make the new list the element into the div
  myDiv.appendChild(ul);
}
<input type="text" style="width:200px" onKeyUp="showNumbers(this)"/>
<div id="my-div">
</div>

Comments

1

You could match digits despite the operator and omit unwanted parts.

const
    string = ' 123/ 4 ',
    values = string.match(/\d+/g).map(Number);

console.log(values);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.