0

I have an array and an object and i would like a function samePrice(arr,obj) like this:

const arr = [0,1,2];
const obj = {
    0: 10,
    1: 10,
    2: 10,
    3: 12,
};

// samePrice(arr,obj) => true

const arr = [0,3];
const obj = {
    0: 10,
    1: 10,
    2: 10,
    3: 12,
};

// samePrice(arr,obj) => false

Here is my current function...

const samePrice = (arr,obj) => {
    let result = true;
  let it = obj[arr[0]];
  arr.forEach(item => {
    if (obj[item] !== it){
      result = false;
    }
  })  
  return result;
}

I am sure there is a better solution.

4
  • 1
    (arr, obj) => false; needs only a single line ... What should the function actually do? Commented Nov 12, 2021 at 16:24
  • The function compares the values of the array with the indexes of the object and returns true if all the values of the object corresponding to the array are equal @Teemu Commented Nov 12, 2021 at 16:28
  • "I have a solution with forEach but the function is 10+ lines long" - you should post that code, even if just as proof of effort (can even be in a folded snippet). As is, the question is indistinguishable from someone just getting their homework done for them. Commented Nov 12, 2021 at 16:38
  • 1
    I understand, but that was because I wasn't very proud of my javascript skills lol. I edited my question with the function I had. @ASDFGerte Commented Nov 12, 2021 at 19:16

4 Answers 4

1

Basically, what you're doing is checking that every element is the same as the first:

const samePrice = (arr,obj) => arr.every(x => obj[x] == obj[arr[0]])

const arr = [0,1,2];
const obj = {
    0: 10,
    1: 10,
    2: 10,
    3: 12,
};

console.log(samePrice(arr,obj)); // => true

const arr2 = [0,3];
const obj2 = {
    0: 10,
    1: 10,
    2: 10,
    3: 12,
};

console.log(samePrice(arr2,obj2)); // => false

Sign up to request clarification or add additional context in comments.

Comments

0

You can use Array.every to test if something is true for all array elements. So get the object value for the first array element, then test if every element is equal to that.

function samePrice(items, prices) {
  if (items.length == 0) {
    return true;
  }
  let price = prices[items[0]];
  return items.every(item => prices[item] == price);
}

const arr1 = [0, 1, 2];
const obj1 = {
  0: 10,
  1: 10,
  2: 10,
  3: 12,
};

console.log(samePrice(arr1, obj1));

const arr2 = [0, 3];
const obj2 = {
  0: 10,
  1: 10,
  2: 10,
  3: 12,
};

console.log(samePrice(arr2, obj2));

Comments

0

I came up with this:

function samePrice(arr, obj) {
  if(arr.length < 1)
    return false;
  i = 1;
  while(i < arr.length) {
    if(obj[arr[i]]!=obj[arr[i-1]])
      return false;
    i++;
  };
  return true;
}

var arr = [0,1,2];
var obj = {
    0: 10,
    1: 10,
    2: 10,
    3: 12,
};

console.log(samePrice(arr, obj));

var arr = [0,3];
var obj = {
    0: 10,
    1: 10,
    2: 10,
    3: 12,
};

console.log(samePrice(arr, obj));

Comments

0

You could separate the first items areference proce and check the rest of the array against it.

const
    samePrice = ([ref, ...a], prices) => a.every(k => prices[ref] === prices[k]);

console.log(samePrice([0, 1, 2], { 0: 10, 1: 10, 2: 10, 3: 12 })); //  true
console.log(samePrice([0, 3], { 0: 10, 1: 10, 2: 10, 3: 12 })); //    false

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.