Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var arr = [1, 2, 3];
- function validateArr(arr){ // validate if items contains only numbers
- var i = 0;
- var l = arr.length;
- var areNumber = true;
- for (i; i< l; i++){ // check if ALL items are numbers, for every "success check" counter ++
- counter = 0;
- if(isNaN(arr[i])){
- areNumber = false;
- }
- else{
- counter++;
- }
- }
- if(counter === l){ // here check if the counter value are equal to length of the array
- areNumber = true;
- }
- return areNumber;
- }
- function sumItems(arr){ // here we sum every item of the array
- var i = 0;
- var l = arr.length;
- var result = 0;
- for (i; i< l; i++){
- result = result + arr[i];
- }
- return result;
- }
- if(validateArr(arr) == true){ // output the result if everything is fine
- console.log(sumItems(arr));
- }
- else{
- console.log("error");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement