skip to Main Content

The problem is that the function sumFunc is not calculating array2 numbers together. Output returns 3 5 7 8 9 11 13 15 16 17 19 20

Under this will be the JS code, output and expected output.

code:

function checkFunc(arr, f1, f2, f3) {
  const containsStrings = arr.some(item => typeof item === 'string');
  const containsNumbers = arr.some(item => typeof item === 'number' && !isNaN(item));

  if (containsStrings && !containsNumbers) {
    console.log('Taulukossa tekstiä. Yhdistetään sanat:');
  } else if (!containsStrings && containsNumbers) {
    console.log('Taulukossa vain lukuja. Lasketaan summa:');
  } else if (containsStrings && containsNumbers) {
    console.log('Taulukossa tekstiä ja numeroita. Yhdistetään sanat:');
  } else {
    console.log('Ei tekstiä tai numeroita');
  }
}

function combineFunc(arr) {
  return arr.join(' ') + (' ');
}

function sumFunc(arr) {
  var sum = 0;
  for (var i = 0; i < arr.length; i++) {
    if (typeof arr[i] === 'number' && !isNaN(arr[i])) {
      sum += arr[i];
    }
  }
  return sum;
}

//cant change code under this 

var array1 = ["Tämän", "taulukon", "alkiot", "muodostavat", "lauseen"];
var array2 = [3, 5, 7, 8, 9, 11, 13, 15, 16, 17, 19, 20];
var array3 = [5, "kertaa", 3, "on", 15];

function someFunction(arr, f1, f2, f3) {
  if (!f1(arr)) {
    console.log(f2(arr));
  } else {
    console.log(f3(arr));
  }
}

someFunction(array1, checkFunc, combineFunc, sumFunc);
someFunction(array2, checkFunc, combineFunc, sumFunc);
someFunction(array3, checkFunc, combineFunc, sumFunc);

output:

Taulukossa tekstiä. Yhdistetään sanat:
Tämän taulukon alkiot muodostavat lauseen 
Taulukossa vain lukuja. Lasketaan summa:
3 5 7 8 9 11 13 15 16 17 19 20 
Taulukossa tekstiä ja numeroita. Yhdistetään sanat:
5 kertaa 3 on 15 

expected output:

Taulukossa tekstiä. Yhdistetään sanat:
Tämän taulukon alkiot muodostavat lauseen 
Taulukossa vain lukuja. Lasketaan summa:
143
Taulukossa tekstiä. Yhdistetään sanat:
5 kertaa 3 on 15 

I tried changing the sumFunc function but it seems like it’s already correct. I also tried .reduce :

function sumFunc(arr) {
    var sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
    return sum; 
}

but that didn’t work. The problem might the somewhere else. I think it might the checkFunc but I’m not sure as I’m starting out with js.

2

Answers


  1. Your checkfunc() is intended to return whether there are only numbers in the array, so, it would return true/false. In your code, your function returns nothing, only prints something in the console.
    I’ve updated it to return true if the array consists only of numbers, false otherwise.

    The correct code should look like:

    
    function checkFunc(arr, f1, f2, f3) {
      const containsStrings = arr.some(item => typeof item === 'string');
      const containsNumbers = arr.some(item => typeof item === 'number' && !isNaN(item));
    
      if (containsStrings && !containsNumbers) {
        console.log('Taulukossa tekstiä. Yhdistetään sanat:');
        return false;
    
      } else if (!containsStrings && containsNumbers) {
        console.log('Taulukossa vain lukuja. Lasketaan summa:');
        return true;
    
      } else if (containsStrings && containsNumbers) {
        console.log('Taulukossa tekstiä ja numeroita. Yhdistetään sanat:');
        return false;
    
      } else {
        console.log('Ei tekstiä tai numeroita');
        return false;
      }
    }
    
    function combineFunc(arr) {
      return arr.join(' ') + (' ');
    }
    
    function sumFunc(arr) {
      var sum = 0;
      for (var i = 0; i < arr.length; i++) {
        if (typeof arr[i] === 'number' && !isNaN(arr[i])) {
          sum += arr[i];
        }
      }
      return sum;
    }
    
    var array1 = ["Tämän", "taulukon", "alkiot", "muodostavat", "lauseen"];
    var array2 = [3, 5, 7, 8, 9, 11, 13, 15, 16, 17, 19, 20];
    var array3 = [5, "kertaa", 3, "on", 15];
    
    function someFunction(arr, f1, f2, f3) {
      if (!f1(arr)) {
        console.log(f2(arr));
      } else {
        console.log(f3(arr));
      }
    }
    
    someFunction(array1, checkFunc, combineFunc, sumFunc);
    someFunction(array2, checkFunc, combineFunc, sumFunc);
    someFunction(array3, checkFunc, combineFunc, sumFunc);
    
    Login or Signup to reply.
  2. you can use this function as if you your method you will get NaN in the console so you can use .reduce method in js

    function sumarry(arr) {
        return arr.reduce((acc,current)=> acc + current , 0);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search