Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !! [link]
      May 24, 2019, 04:20:48 PM
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !! [link]
      May 24, 2019, 04:20:40 PM
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !! [link]
      May 24, 2019, 04:20:31 PM
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !! [link]
      May 24, 2019, 04:20:21 PM
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !! [link]
      May 24, 2019, 04:20:14 PM
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !! [link]
      May 24, 2019, 04:20:07 PM
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !! [link]
      May 24, 2019, 04:19:59 PM
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !! [link]
      May 24, 2019, 04:19:50 PM
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT ! ! [link]
      May 24, 2019, 04:19:33 PM
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !!https://www.dissuade614.com/
      May 24, 2019, 04:18:54 PM
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !!https://www.dissuade614.com/
      May 24, 2019, 04:18:26 PM
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !!https://www.dissuade614.com/
      May 24, 2019, 04:18:14 PM
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !!https://www.dissuade614.com/
      May 24, 2019, 04:17:57 PM
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !!https://www.dissuade614.com/
      May 24, 2019, 04:17:46 PM
    • kingyou: DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !!https://www.dissuade614.com/
      May 24, 2019, 04:17:34 PM
    • kingyou:[link]
      May 24, 2019, 04:17:05 PM
    • kingyou:[link]  DID YOU ENJOY [email protected]!!!! WELL THIS IS WAYYYY BETTER ! COME TRY US OUT !!!
      May 24, 2019, 04:16:58 PM
    • PavSwag: BRAND NEW RSPS AUTOSCAPE,ORG FREE MBOX WHEN YOU GET 99 SKILL FOR DONATOR POINTS
      May 22, 2019, 01:52:56 PM
    • Duriall: Join the HYPE TRAIN! New Server - Varrock - Release is tomorrow. Join the discord and forums now for free items on release! [link]
      May 21, 2019, 04:47:56 PM
    • Riskdemon:[link] Varrock OSRS RSPS releasing May 22nd! Discord link provided!
      May 18, 2019, 11:43:14 PM

    Author Topic: Javascript Method/Array Questions  (Read 1872 times)

    0 Members and 1 Guest are viewing this topic.

    OfflineAdobe

    • Member
    • ****
    • Posts: 302
    • Thanks: +0/-0
      • View Profile
    Javascript Method/Array Questions
    « on: November 29, 2016, 04:04:51 PM »
    Hello, I'm a freshman in college for CS looking to end up somewhere on the web development end of things. Currently, in one of my classes it is strictly JavaScript and I am stuck on "For Fun" projects that my professor has given us to learn from.

    The commented code are the instructions for the problems. The code in between the comments are what I have done. I know the first part I have correct, but the rest I could just use some guidance on how to finish.

    Code: [Select]
    #!/usr/local/bin/node

    /**
     * Opens and reads the file given as the first command line argument and puts
     * it's contents into "data":
     */

    const fs = require("fs");
    var data = fs.readFileSync(process.argv[2], {encoding: 'utf8'});

    /**
     * data above is a JSON encoded string of an array of numbers, use the
     * JSON.parse() method to turn it back into an array of numbers stored in the
     * variable a:
     */

    var a = data.parse();

    /**
     * Loop through the array and find the smallest value within and print it out.
     * Example input/output:
     * ./l1.js testdata
     * -488
     */
     
    for(var i=0; i < a.length; i++){
    console.log(a[i]);
    }
    var testdata = a.length;

    // ^ OR v OR neither, don't know for sure.

    for(var testdata in a){
    if(a.length < testdata){
    console.log(testdata + " " + a[testdata]);
    }
    }
    rep means nothing on this forum, so don't let it get to your head.
    Runescape Gambling

    OfflinestCky

    • Member
    • ****
    • Posts: 409
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #1 on: November 29, 2016, 04:14:05 PM »
    I would lean towards the first loop you're writing, here's some psuedo-code to point you in the right direction:

    Code: [Select]
    // First set a variable which will hold the lowest number for printing later - we'll set it to the first data entry for something to compare against
    var lowestValue = { first data entry }

    for(var i=0; i < a.length; i++){ // starting at zero, count up to the total number of data entries
        var currentEntryValue = { current data entry }
        if(currentEntryValue is less than lowestValue){
            lowestValue = currentEntryValue // set the lowest value to current as the current one is lower
        }
     
    }

    // print the lowest value


    HOLD CTRL AND TYPE "WTF" FOR ℱ𝓪𝓷𝓬𝔂 𝓦𝓣ℱ
    Runescape Gambling

    OfflineAdobe

    • Member
    • ****
    • Posts: 302
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #2 on: November 30, 2016, 12:11:32 PM »
    Thank you!

    I was able to change your provided code into what mine needed to be.

    Final result:

    Code: [Select]
    var lowestValue = a[0];

    for(var i=0; i<a.length; i++){
        if(a[i]< lowestValue){
            lowestValue = a[i];
        }
    }

    //print lowest value

    I am now working on the next process after this problem. Although, I am having trouble with it as well.

    Code: [Select]
    #!/usr/local/bin/node

    /**
     * Open and read the file given as the first command line argument and put
     * it's contents into "data":
     */
    const fs = require("fs");
    var data = fs.readFileSync(process.argv[2], {encoding: 'utf8'});


    /**
     * data above is a JSON encoded string of an array of numbers, use the
     * JSON.parse() method to turn it back into an array of numbers stored in the
     * variable a:
     */

    var a = JSON.parse(data);


    /**
     * Loop through the array and find the _second_ smallest value within and print
     * it out.
     * Example input/output:
     * ./l2.js testdata
     * -481
     */

    var lowestValue = a[0];
    var secondLowestValue = a[1];
     
    for(var i=0; i < a.length; i++){
    for(var j=0; j < a.length; j++){
        if(a[i] < lowestValue){
    lowestValue = a[i];

    if(a[j] < secondLowestValue){
        secondLowestValue = a[j];
               }
           }
        }
    }

    console.log(secondLowestValue);

    The current commented out code are the instructions, and the code inbetween are what I have tried. The code after the third commented instructions are what I need help on. The result I have been getting is -157 when it should be, according to instructions, is -481.
    rep means nothing on this forum, so don't let it get to your head.
    Runescape Gambling

    OfflinestCky

    • Member
    • ****
    • Posts: 409
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #3 on: November 30, 2016, 02:35:58 PM »
    Good shit.

    You don't need two loops, as you're only counting through one set of data.

    Using my last example as a base, I've added some more psuedo code and instructions to point you to a possible solution.

    It is much simpler and closer to your first problems solution than you may think.

    Code: [Select]
    // First set a variable which will hold the lowest number for printing later - we'll set it to the first data entry for something to compare against
    var lowestValue = { first data entry }

    // This variable will hold the second lowest value
    var secondLowestValue = { first data entry }

    for(var i=0; i < a.length; i++){ // starting at zero, count up to the total number of data entries

        var currentEntryValue = { current data entry }

        if(currentEntryValue is less than lowestValue){
            // at this point we know we have a new lowest value, so the last lowestValue is the second lowest!
            // set the secondLowestValue to match the lowestValue

            // then set the new lowest value
            lowestValue = currentEntryValue // set the lowest value to current as the current one is lower
        }


    }

    // print the lowest value
    // print the second lowest value

    HOLD CTRL AND TYPE "WTF" FOR ℱ𝓪𝓷𝓬𝔂 𝓦𝓣ℱ

    Offline0wais

    • First Time Poster
    • *
    • Posts: 2
    • Thanks: +10/-10
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #4 on: November 30, 2016, 02:50:26 PM »
    Hello Stcky,
    Mind activating my account?
    thanks
    0wais

    OfflinestCky

    • Member
    • ****
    • Posts: 409
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #5 on: November 30, 2016, 02:54:01 PM »
    This is not the correct thread for such a request nor am I a member of staff. Hate to break it to ya but they probably don't really care dude.
    HOLD CTRL AND TYPE "WTF" FOR ℱ𝓪𝓷𝓬𝔂 𝓦𝓣ℱ

    OfflineAdobe

    • Member
    • ****
    • Posts: 302
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #6 on: November 30, 2016, 05:15:51 PM »
    Good shit.

    You don't need two loops, as you're only counting through one set of data.

    Using my last example as a base, I've added some more psuedo code and instructions to point you to a possible solution.

    It is much simpler and closer to your first problems solution than you may think.

    Code: [Select]
    // First set a variable which will hold the lowest number for printing later - we'll set it to the first data entry for something to compare against
    var lowestValue = { first data entry }

    // This variable will hold the second lowest value
    var secondLowestValue = { first data entry }

    for(var i=0; i < a.length; i++){ // starting at zero, count up to the total number of data entries

        var currentEntryValue = { current data entry }

        if(currentEntryValue is less than lowestValue){
            // at this point we know we have a new lowest value, so the last lowestValue is the second lowest!
            // set the secondLowestValue to match the lowestValue

            // then set the new lowest value
            lowestValue = currentEntryValue // set the lowest value to current as the current one is lower
        }


    }

    // print the lowest value
    // print the second lowest value

    Thanks again for going at it. I've gotten the results closer to that of which I need, but have yet to actually get it correct yet.

    Now, with this code, I am getting -431, which is closer to -481 than -157 is so progress is being made.

    Code: [Select]

    var lowestValue = a[0];
    var secondLowestValue = a[0];
     
    for(var i=0; i < a.length; i++){
        if(a[i] < lowestValue){
            lowestValue = a[i];
            secondLowestValue = lowestValue;
        }
        lowestValue = a[i];
    }

    console.log(secondLowestValue);

    « Last Edit: November 30, 2016, 05:18:16 PM by Adobe »
    rep means nothing on this forum, so don't let it get to your head.

    OfflinestCky

    • Member
    • ****
    • Posts: 409
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #7 on: November 30, 2016, 05:56:00 PM »
    In this latest example you're resetting lowestValue to the latest entry every time as seen in the last line of the loop. You only want to set lowestValue again if the current entry is actually lower than the current lowestValue.

    You also need to set the secondLowestValue to the current lowestValue BEFORE you set the lowest value to the new current entry, as lowestValue will be changing to the new entry obviously - the point is we're retaining the value in secondLowestValue before the lowestValue is updated.
    HOLD CTRL AND TYPE "WTF" FOR ℱ𝓪𝓷𝓬𝔂 𝓦𝓣ℱ

    OfflineAdobe

    • Member
    • ****
    • Posts: 302
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #8 on: December 01, 2016, 06:41:47 AM »
    In this latest example you're resetting lowestValue to the latest entry every time as seen in the last line of the loop. You only want to set lowestValue again if the current entry is actually lower than the current lowestValue.

    You also need to set the secondLowestValue to the current lowestValue BEFORE you set the lowest value to the new current entry, as lowestValue will be changing to the new entry obviously - the point is we're retaining the value in secondLowestValue before the lowestValue is updated.

    Thank you, yet again!

    Simple fix to solve many issues.

    Code: [Select]
    var lowestValue = a[0];
    var secondLowestValue = a[0];
     
    for(var i=0; i < a.length; i++){
        if(a[i] < lowestValue){
            secondLowestValue = lowestValue;
            lowestValue = a[i];
        }
    }

    console.log(secondLowestValue);


    My next problem I thought was going to be easy. But often for some, easier said than done.

    Code: [Select]
    /**
     * Make a box of *'s that is n columns by m rows in size:
     * Example input/output:
     * ./l5.js 5 4
     * *****
     * *****
     * *****
     * *****
     */

    var num_rows = m;
    var num_cols = n;
    var symb = '';

    for(var i=0; i<num_rows; i++){
        symb += '*';
        for(var j=0; j<num_cols; j++){
            symb += '*';
            symb +=  i + j;
        }
        console.log(symb);
    }

    The commented code are the instructions, below those are the code I have attempted. So far, my output is this:



    Which isn't exactly what I'm looking for.

    I was able to get the numbers to fade away commenting this line out:

    « Last Edit: December 01, 2016, 07:08:12 AM by Adobe »
    rep means nothing on this forum, so don't let it get to your head.

    OfflinestCky

    • Member
    • ****
    • Posts: 409
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #9 on: December 11, 2016, 08:12:19 PM »
    did u win
    HOLD CTRL AND TYPE "WTF" FOR ℱ𝓪𝓷𝓬𝔂 𝓦𝓣ℱ

     

    Copyright © 2017 MoparScape. All rights reserved.
    Powered by SMFPacks SEO Pro Mod |
    SimplePortal 2.3.5 © 2008-2012, SimplePortal