Array - transform strings to numbers

Hi,

I made an JS exercise, that is a number analyzer, but I am struggling to get the string elements as a number, and not as a string.

What Am I doing wrong?

Repository
Published website

I think that the wrong max, min, sum and average has all to do with it.

Can anyone help me?

Here is also the code image

I think that you need to coerce the string to a numerical value, in order to perform arithmetical functions.
You could perhaps do this most simply at the top of your add function, using a unary plus operator (+)

let n = +number.value;

Thanks. it worked.

But it is not sorting the array correctly.

With the list.sort() shouldn’t the array be sorted from the lowest number to the highest number?
Since I try to find the highest number, the last one in the string, and the lowest, the first one, this is important.

Please post actual code instead of a picture.

1 Like
let number = document.querySelector("input#txtnum");
let sel = document.querySelector("select#sel");
let res = document.querySelector("p#res");
let list = [];




function add() {
    let n = +number.value;
    list.sort();     

    if (n < 1 || n > 100 || list.includes(n)) {
        alert("Invalid Value or already added to the list");
    } else {
        list.push(n);        
        let item = document.createElement("option");
        item.text = `Number ${n} added`; 
        sel.appendChild(item);
        res.innerHTML = ""
               
    }
    number.value = "";
    number.focus();
    
  }



function analyze() {
       
    let total = list.length;
    let max = list[list.length - 1];
    let min = list[0];
    let sum = 0;
    for (let i = 0; i < list.length; i++) {
        sum += list[i];
    }
    let avg = sum / total;

    res.innerHTML = "";
    res.innerHTML += `In total, we have ${total} numbers added <br><br>`;
    res.innerHTML += `The highest number is ${min} <br><br>`;
    res.innerHTML += `The lowest number is ${max} <br><br>`;
    res.innerHTML += `The sum of all values ${sum} <br><br>`;
    res.innerHTML += `The average is ${avg} <br><br>`;
    
      }

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

ups. sorry. Tks for the advice and correction.

I would read up on how sort actually works.

array.sort() is alphabetical by default. You need to include a comparison function for a numerical sort.

let listSorted = list.sort((a,b)=>a-b);

thanks a lot. This solved the problem.
Number analyzer

But I need to understand better what the

=>

means

That is arrow (lambda) function syntax, introduced in ES6.
These are equivalent:

(a, b) => a - b
function (a, b) { return  a - b }

It’s shorthand for traditional function syntax (with limitations).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.