Create a checking process

homework
javascript

#1

Hi everyone, I need your help with a small problem that I can't find a solution even after finishing the whole course. As an assignment, I need to write a script that would check multiple objects which contain only one value each like an SKU similar to this: [color id, size, quantity] (note the comma is count along as a whole string, not a separator in coding.

I need to check if the particular size has the required quantity, for example:
Object 1 has a value of [0001,S,2 | 0001,M,8 | 0001,L,8]
- Check if small size must always have 2 items:
if Object 1 has this "S,2" in its value => return true. Otherwise return false for any other scenarios.

Then continue to check the medium size, and so on. The main problem is I don't know how to search a specific letter like "S,2" inside of an object's value. Please help me to suggest a solution that can either break the value into sub strings for Small, Medium and Large size OR like a function that search a specific letter similar to a Left, Mid, Right functions in Excel.


#2

Something like this?

function s2Search(searchstring, object1) {
  if (object1.search(searchstring) != -1) {
    return true;
  } else {
    return false;
  }
}
s2Search("S,2","0001,S,2 | 0001,M,8 | 0001,L,8");

#3

Thanks for your help,

Your code helped me to find and check a single value inside the Object. However, to be more efficient, my tasks is extended to a much challenging requirement. To check the Object's ID if each size meets the standard criteria by manipulating Google sheets with Google Apps Scripts, for example:
- Requirements: Each size (S,M,L,XL) must have the corresponding quantity of (2,4,6,2)
- So if object 1 has these color id, sizes and quantity [S,2 | M,4 | L,6 | XL,2] ==> return true
- if object 2 has these id [S,2 | M,4 | L,8 | XL,2] ==> Logger.log ("It is not match with standard")

If you got any advice for a newbie, I am all ear since I have spend hours searching for solutions and you seem my best so far.


#4

Okay i didnt know how you get your data.

With an array i would do it like this, but i dont know how the performance is.

var object = ["[S,2 | M,4 | L,8 | XL,2]","[S,2 | M,4 | L,6 | XL,3]"];
var search = object.indexOf("[S,2 | M,4 | L,6 | XL,2]");

var match = function(search){
	if(search === -1 ){
		console.log("It is not match with standard");
	} else {
		return true;
	}
}

match(search);


#5

Things are cleared from my side,

Thank you for your assistance, and I could have not done it without you !

Wish you all the best,


#6

Nice to hear, you are welcome.