How do I write a script that will find the arithmetic mean?

  • 0
    Good afternoon, I am having a problem while writing code.
    Create a script that asks the user for a number (any amount) and finds the arithmetic mean of the numbers.
    The script returns both the average and all the numbers entered by the user.
    Be sure to use the array object.
    Here is my code example:

    var number = prompt ("Enter numbers (sufficient number)", "");
    var arr = new Array (number)
    var middle;
    var words = number.split ("");
    var lenght1 = words.length;
    lenght1 = parseInt (lenght1)
    var num1;
    var num2;
    var num3;
    do {
    num1 = words.shift
    num1 = parseInt (num1)
    num2 = words.pop
    num2 = parseInt (num2)
    num3 = num1 + num2
    n = words.length
    while (n & gt; 0)
    document.writeln (num3)
    JavaScript Graham Lindsey, Mar 25, 2020

  • 2 Answers
  • 0
    You are doing a lot of unnecessary actions.

    var inputNumbers = prompt("Введіть числа(довільна кількість)", ""); // получили строку чисел. Предположительно разделённых пробелом.
    var numbers = inputNumbers.split(" "); // Получили массив чисел в виде строк.
    var summ = numbers.reduce(function(summ, item){return summ += parseInt(item)}, 0) // используем метод массивов reduce внутри которого преобразуем каждое строковое число в number и складываем.
    var result = summ / numbers.length // делим сумму на количество

  • 0
    I don't know js, but here is the python code:

    def getAverage(numArray):
    total = 0
    for num in array:
    total += num

    return total/len(numArray)

    def getArray():
    length = print('Введите количество чисел: ')
    array = []
    for x in range(0, length):
    array.append(input('Введите число'))

    return array

    def main():
    array = getArray()
    average = getAverage(array)

    Atticus Gross

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!