Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Using a switch statement to count variables in an array

Using a switch statement to count variables in an array

Scheduled Pinned Locked Moved JavaScript
javascriptdata-structures
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tristarterror
    wrote on last edited by
    #1

    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.

    L L M 3 Replies Last reply
    0
    • T tristarterror

      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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      loop through the array and use switch on each element to increment the number of negative, zero and positive values..hope this helps..

      1 Reply Last reply
      0
      • T tristarterror

        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.

        L Offline
        L Offline
        lmf232s
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        • T tristarterror

          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.

          M Offline
          M Offline
          Manfred Rudolf Bihy
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups