0

I have two immutable arrays. One is ordinary ([1,2,3,4]), the other is multiplied by two ([2,4,6,8]). How in the test to equalize each value of the first array with the value of the second I use the iteration? That 1 is 2, and 2 is 4 and so on. I think this can be done with a for loop, but I do not know how to write this in practice.

import { List, Set } from "immutable"

export function mass() {
  let standardArray = List([1,2,3,4]);
  let mutatedArray = standardArray.map(x => x * 2);
  return mutatedArray;
};

test code (I do not know how to proceed)

import { List, Set, isImmutable, Record, Map } from "immutable"
import { mass } from "./sum";

test('Array Multiplication Test', () => {
  let standardArray = List([1,2,3,4]);
  let mutatedArray = standardArray.map(x => x * 2);
  expect(standardArray).not.toEqual(mutatedArray);
});
skyboyer
  • 22,209
  • 7
  • 57
  • 64
deniz
  • 7
  • 2
  • 1
    Can you explain more about **How in the test to equalize each value of the first array with the value of the second I use the iteration? That 1 is 2, and 2 is 3 and so on** – Isaac Jul 11 '18 at 09:26
  • Specifically on **and 2 is 3** ? – Isaac Jul 11 '18 at 09:27
  • It sounds like you just want to compare two arrays to check if they're not the same? – George Jul 11 '18 at 09:27
  • 1
    Possible duplicate of [How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – George Jul 11 '18 at 09:29
  • There are two arrays and you need to compare them. The test should check that all values of the first array correspond to the whole value of the second array multiplied by 2. – deniz Jul 11 '18 at 09:29
  • 2
    then 2 should be 4 right? why 3? – Isaac Jul 11 '18 at 09:30
  • I made a mistake 1 is 2 2 is 4 3 is 6 and so on – deniz Jul 11 '18 at 09:31
  • If this is a test case for you code and not just a coding exercise, it seems pretty pointless to test that multiplication works unless you have statically defined data, as you're still using `*` to create and test the data – George Jul 11 '18 at 09:34

2 Answers2

0

Why not make use of a flag. Take a flag variable and set it to any value, loop through the array and if any value does not meet expectations, change the flag variable. In the end, check the value of flag variable. If it is the same as set initially then they are same otherwise the arrays are different.

import { List, Set, isImmutable, Record, Map } from "immutable"
import { mass } from "./sum";

test('Array Multiplication Test', () => {
  let standardArray = List([1,2,3,4]);
  let mutatedArray = standardArray.map(x => x * 2);
  let flag = false;
  for (let i = 0; i < standardArray.length; i++) {
    if (standardArray[i] * 2 != mutatedArray[i]) {
      flag = true;
      break;
    }
  }
  expect(flag).toEqual(false);
});
Aayush Sharma
  • 779
  • 4
  • 20
  • that in the loop was expect and every time checked the value, if 1 is equal to 2 is true, and 2 is not equal to 4, then an error occurred – deniz Jul 11 '18 at 09:57
0

I think you want something like this:

let testArr1 = [1,2,3,4]
let testArr2 = [2,4,6,8]
let testArr3 = [2,6,6,8]

function doesMatchCriteria(arr1, arr2) {
  if (arr1.length !== arr2.length) return false;
  return arr1.every((e, i) => ((e*2) === arr2[i]));
  return true;
}
console.log(doesMatchCriteria(testArr1, testArr2));  // true
console.log(doesMatchCriteria(testArr1, testArr3));  // false

So, your test function could be something like this:

test('Array Multiplication Test', () => {
  let testArr1 = [1,2,3,4]
  let testArr2 = [2,4,6,8]
  expect(doesMatchCriteria(testArr1, testArr2)).toBe(true);
});      

Don't forget to define doesMatchCriteria function in the same file.

BlackBeard
  • 10,246
  • 7
  • 52
  • 62