www.webdeveloper.com
Results 1 to 4 of 4

Thread: New at this

Hybrid View

  1. #1
    Join Date
    Mar 2013
    Posts
    2

    New at this

    I need help. I am taking an intro course in JS and I need help with some practice questions from my textbook that I can't get to work on my computer.

    I have to write 3 functions with while loops in them. One to count the number of whitespaces in a string (my teacher recommended accessing it like an array but I haven't been able to do that), one that takes in two numbers and sees how many times the first goes into the second by adding, and the last one is to see if an array contains any positive numbers. My code is below for what I have done. I appreciate any guidance and assistance. Thanks!

    Code:
    count = 0;
    var i = 0;
    
    function howManySpaces(input) 
    {
      while(i < input.length-1){
        if {(howManySpaces[index] = '')
          count++;
          i++ }
        else {
          i++ }
      }
    }
    Code:
    var count=0;
    var c=0;
    function howManyFit(a, b) {
    while ((b - c) >= a){
    c = c + a;
    count++;
    }
    return count;
    }
    Code:
    Nothing for the positive check. I've been playing around with the modulus but haven't gotten it to work at all.

  2. #2
    Join Date
    Mar 2013
    Posts
    2
    Edit: The last function returns false if there is a negative or 0 in the array

  3. #3
    Join Date
    Feb 2013
    Posts
    5
    Hello
    Code:
    count = 0;
    var i = 0;
    
    function howManySpaces(input) 
    {
      while(i < input.length-1){
        if {(howManySpaces[index] = '')
          count++;
          i++ }
        else {
          i++ }
      }
    }
    I think the next part is completly wrong
    Code:
    if {(howManySpaces[index] = '')
    - There is no argument for the "if". if (arguments) {code to excute}.
    -There is no array named "howManySpaces".
    - "index" is undefined.
    -'' indicate empty. Space ' '.
    I think the code could be something like:
    Code:
    var count = 0;
    var i = 0;
    
    function howManySpaces(n) {
      while(i < n.length-1){
        if (n[i] = ' '){
          count++;
          i++ ;}
        else {
          i++ ;}
      }
    return count;
    }
    spaces= howManySpaces(input);
    But maybe it's better to use regular expression to test agains all white spaces.
    Something like:
    Code:
    var spaceChar= new RegExp ('\\s');
    if (spaceChar.test(n[i]) {
    ...

  4. #4
    Join Date
    Feb 2013
    Posts
    5
    Edit: To iterate through all the array elements you should use (n.length) insted of (n.length-1).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles