This code that will create a p element for every index in an array and it will put a parantheses on the lowest and highest number in an array
const liArray = [5.6, 8.7, 1.3, 10, 56];
const min = Math.min.apply(Math, liArray);
const max = Math.max.apply(Math, liArray);
const parentElement = document.getElementById("myDiv");
liArray.forEach((currentValue, index) => {
const elm = document.createElement('p');
let text = `${index + 1}. ${(currentValue == min || currentValue == max) ? `(${currentValue})` : currentValue}`;
elm.innerText = text;
parentElement.appendChild(elm);
});
<div id="myDiv"></div>
but my array is like this
const liArray = ["5.6 hello", "8.7 hi", "1.3 hey", "10 hi", "56 hello"]
and i want to also add parentheses to the lowest and highest number and output like this
1. 5.6 hello
2. 8.7 hi
3. (1.3) hey
4. 10 hi
5. (56) hello
Here is what i tried :
i tried this code that seperates the numbers and letters into 2 different arrays so i can apply the max and min to the numbers
const liArray = ["5.6 hello", "8.7 hi", "1.3 hey", "10 hi", "56 hello"];
const parentElement = document.getElementById("myDiv");
const getNumbers = liArray.map((i) => Number(i.replace(/[^0-9.]/g, "")));
const getWords = liArray.map((i) => i.replace(/[0-9.]/g, ""));
const min = Math.min.apply(Math, getNumbers);
const max = Math.max.apply(Math, getNumbers);
getNumbers.forEach((currentValue, index) => {
const elm = document.createElement('p');
let text = `${index + 1}. ${(currentValue == min || currentValue == max) ?
`(${currentValue})` : currentValue}. ${getWords}`;
elm.innerText = text;
parentElement.appendChild(elm);
});
but it outputs this instead
2. 8.7. hello, hi, hey, hi, hello
3. (1.3). hello, hi, hey, hi, hello
4. 10. hello, hi, hey, hi, hello
5. (56). hello, hi, hey, hi, hello