Using a switch statement to count variables in an array
-
Another homework assignment so please just let me know what direction I should go in. Assignment details: Function: counter Parameter: An array of numbers Returns: The numbers of negative elements, zeros, and values greater than zero in the given array. Note: You must use a switch statement in the function. My JS file:
// Array of Numbers
var nums = new Array(-13,2,67,3,0,56,12,-41);
var len = nums.length;for (i=0; i<len; i++) {
var count = nums[i];
}var count = nums[i];
//Create counter function
function counter() {switch (count) { case "negatives": if (count < 0) { document.write(count.length); } break; case "zeros": if (count == 0) { document.write(count.length); } break; case "positives": if (count > 0) { document.write(count.length); } break; default: { document.write("nothing!"); }
}};
I am not sure where to go with this one. I know it's the switch statement that throwing me off and I am not sure if my statement for counting each variable is necessarily correct either but I feel like I'm somewhere in the correct thinking here.
-
Another homework assignment so please just let me know what direction I should go in. Assignment details: Function: counter Parameter: An array of numbers Returns: The numbers of negative elements, zeros, and values greater than zero in the given array. Note: You must use a switch statement in the function. My JS file:
// Array of Numbers
var nums = new Array(-13,2,67,3,0,56,12,-41);
var len = nums.length;for (i=0; i<len; i++) {
var count = nums[i];
}var count = nums[i];
//Create counter function
function counter() {switch (count) { case "negatives": if (count < 0) { document.write(count.length); } break; case "zeros": if (count == 0) { document.write(count.length); } break; case "positives": if (count > 0) { document.write(count.length); } break; default: { document.write("nothing!"); }
}};
I am not sure where to go with this one. I know it's the switch statement that throwing me off and I am not sure if my statement for counting each variable is necessarily correct either but I feel like I'm somewhere in the correct thinking here.
-
Another homework assignment so please just let me know what direction I should go in. Assignment details: Function: counter Parameter: An array of numbers Returns: The numbers of negative elements, zeros, and values greater than zero in the given array. Note: You must use a switch statement in the function. My JS file:
// Array of Numbers
var nums = new Array(-13,2,67,3,0,56,12,-41);
var len = nums.length;for (i=0; i<len; i++) {
var count = nums[i];
}var count = nums[i];
//Create counter function
function counter() {switch (count) { case "negatives": if (count < 0) { document.write(count.length); } break; case "zeros": if (count == 0) { document.write(count.length); } break; case "positives": if (count > 0) { document.write(count.length); } break; default: { document.write("nothing!"); }
}};
I am not sure where to go with this one. I know it's the switch statement that throwing me off and I am not sure if my statement for counting each variable is necessarily correct either but I feel like I'm somewhere in the correct thinking here.
You have all the parts but need to rework them a bit. I can't see how your calling the function but how about you pass the calculate function the array and then do your foreach with in the loop. In the end you'll want to keep track of each element individually so you can properlly account for them. Currenlty your just doing a document.write but that could also be for debugging purposes. Good Luck!
-
Another homework assignment so please just let me know what direction I should go in. Assignment details: Function: counter Parameter: An array of numbers Returns: The numbers of negative elements, zeros, and values greater than zero in the given array. Note: You must use a switch statement in the function. My JS file:
// Array of Numbers
var nums = new Array(-13,2,67,3,0,56,12,-41);
var len = nums.length;for (i=0; i<len; i++) {
var count = nums[i];
}var count = nums[i];
//Create counter function
function counter() {switch (count) { case "negatives": if (count < 0) { document.write(count.length); } break; case "zeros": if (count == 0) { document.write(count.length); } break; case "positives": if (count > 0) { document.write(count.length); } break; default: { document.write("nothing!"); }
}};
I am not sure where to go with this one. I know it's the switch statement that throwing me off and I am not sure if my statement for counting each variable is necessarily correct either but I feel like I'm somewhere in the correct thinking here.
Use the sign function the values of which you can use in your switch statement:
// Array of Numbers
var nums = new Array(-13,2,67,3,0,56,12,-41);
var len = nums.length;var positives = 0;
var negatives = 0;
var zeroes = 0;for (i=0; i<len; i++) {
counter(num[i];
}// Here you need to output the values positives, zeroes and negatives to show which one occurred how many times
//Create counter function
function counter(number) {var sign = (number > 0) ? 1 : (number < 0) ? -1 : 0; switch (sign) { case 1: positives++ break; case 0: zeroes++; break; case -1: negatives++; break;
};
Regards,
— Manfred
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian