The in_array function implemented by JavaScript, javascriptin_array
In JS, You need to determine whether a value is directly used by no function in the array. For example, the in_array () function exists in PHP. However, we can write a function similar to in_array () to determine whether a value is in the function.
/*** JS determines whether a value exists in the array * // defines a judgment function var in_array = function (arr) {// determine whether the parameter is an array var isArr = arr & console. log (typeof arr === 'object '? Arr. constructor === Array? Arr. length? Arr. length = 1? Arr [0]: arr. join (','): 'an empty array ': arr. constructor: typeof arr); // if (! IsArr) {throw "arguments is not Array";} // indicates whether the traversal is in the Array for (var I = 0, k = arr. length; I <k; I ++) {if (this = arr [I]) {return true ;}} // if it is not in the array, false return false will be returned;} // Add the prototype String to the String. prototype. in_array = in_array; // Add the Prototype Number to the numeric type. prototype. in_array = in_array; // declare an Array var arr = Array ('blue', 'red', '123', '123 '); // string test var str = 'red'; var isInArray = str. in_array (arr); alert (isInArray); // true // digital test var num = 119; var isInArray = num. in_array (arr); alert (isInArray); // false
If the input is not an array, an exception is thrown.
/*** JS determines whether a value exists in the array * // defines a judgment function var in_array = function (arr) {// determine whether the parameter is an array var isArr = arr & console. log (typeof arr === 'object '? Arr. constructor === Array? Arr. length? Arr. length = 1? Arr [0]: arr. join (','): 'an empty array ': arr. constructor: typeof arr); // if (! IsArr) {throw "arguments is not Array";} // indicates whether the traversal is in the Array for (var I = 0, k = arr. length; I <k; I ++) {if (this = arr [I]) {return true ;}} // if it is not in the array, false return false will be returned;} // Add the prototype String to the String. prototype. in_array = in_array; // Add the Prototype Number to the numeric type. prototype. in_array = in_array; // declare an array var arr = null; // string test var str = 'red'; var isInArray = str. in_array (arr); alert (isInArray); // uncaught exception: arguments is not Array
Direct error:
What is the function of adding two numbers in javascript?
Since priceArray [I]. value is a string, if you add it directly, it is processed by string connection. You should first convert it to a number:
TotalPrice = totalPrice + parseInt (priceArray [I]. value );
In fact, the standard javascript syntax is:
TotalPrice + = parseInt (priceArray [I]. value );
In javascript, how can we customize functions to implement the push function of Array objects?
Follow your procedures
This line
New_push (arr, "20"); // use a custom function to add new array elements
Should be changed to arr = new_push (arr, "20 ");