DevDocs

Software Engineering

THe Complete Course

Designed by Kerwin THompson

Start

This document was created as a repository for all of our code snippets used on various projects. This includes:

JS: WHY DO WE NEED ARRAYS?

Sam has arrived and is rolling a wheelbarrow full of mysterious objects.

You're wondering what all these objects are for, but before you even have a chance to ask, Sam is already looking over your shoulder to see what you're working on.

"Oh wow, arrays! I've recently finished Sprint 5 myself and I can tell you, these things are super useful. They're just like my wheelbarrow: I'd have such a hard time moving so many objects around, but here they are, packed neatly and as easy to move as a single object."

Sam is very excited about the opportunity to give you a little lecture on arrays. "May I borrow your laptop for a moment?" Sam asks, then continues:

"Arrays are used to store a collection of values of the same type. I'll show you an example. Consider the following image showing a drop-down list of search suggestions:

A search bar on the Yandex search page. 'Javascript' is typed into the search bar and there are several drop-down autocomplete suggestions for Javascript.

The search bar suggests relevant search terms using autocomplete

The search bar suggests relevant search terms using autocomplete

We could declare a separate variable for each item in the list:

        
            const suggestion1 = "javascript";
const suggestion2 = "javascript download";
const suggestion3 = "javascript: void 0";
const suggestion4 = "javascript for beginners";
const suggestion5 = "javascript error: wall is not defined"; 
        

But this is very inconvenient for a number of reasons. Even doing something quite simple like arranging all the elements in alphabetical order would be tiresome, because you'd have to write a piece of code that analyzes each variable individually.

Plus, there could be a lot of search suggestions, and you can't predict exactly how many there will be. Using an array addresses all these issues:

        
            const suggestions = [
            "javascript",
            "javascript download",
            "javascript: void 0",
            "javascript for beginners",
            "javascript error: wall is not defined"
          ]; 
        

Arrays are the perfect way of dealing with any list of elements of the same type. This can be anything from search suggestions to items in a To-Do list, or definitions in a glossary.

Even Greta Thunberg's Twitter feed is an array. Her tweets can be written as an array of objects in JavaScript:

    
const tweets = [
  {
    username: "Greta Thunberg",
    date: "8/27/2021",
    text: "School strike week 158.",
    retweets: 1861,
    comments: 281,
    likes: 13478
  },
  {
    username: "Greta Thunberg",
    date: "8/20/2021",
    text: "School strike week 157.",
    retweets: 1594,
    comments: 408,
    likes: 9338
  },
  {
    username: "Greta Thunberg",
    date: "8/6/2021",
    text: "#HejaSverige !",
    retweets: 1476,
    comments: 255,
    likes: 17695
  }
]; 

In this chapter, you'll work with arrays and learn how to do different things with them, like:

JavaScript has built-in methods to help you do all these things, and that's what you're going to learn about now. While you get started on arrays, I'll go unload my wheelbarrow.

Hej då! (This means "bye" in Swedish. I've been learning it cause I hope to start a sustainable artistic commune in Stockholm one of these days.

>They say software engineers often work remotely, right?)"

JS: CONCATENATING ARRAYS AND JOINING ELEMENTS IN A STRING

Code Description

This can be achieved using string manipulation functions in PHP. First we find the position of the desired character in the string using strpos(), substr(), ltrim, and trim()

Before

            
        $new_string = substr($old_string, strpos($old_string, '_') + 1);
            
        

or

            
        $new_string = ltrim(stristr($old_string, '_'), '_');
            
        

After

            
                $new_string = substr($old_string, 0, strpos($old_string, "-"));
            
        

How this code works?

The above code was used to fix a problem with the global variable $_GET[] when calling pages on the index.php file for our back-end applications. The code was used to check the string in the url after index.php? by removing the question sign (?) before it and any other sign after it primarily an equal sign or an ampersand after it. The global variable $_SERVER["REQUEST_URI"] was used to grab the url. The process required two stages.

Part 1: Remove question mark and everything before it

            
//get ur
//global variable:
$_SERVER["REQUEST_URI"] = "/biz/cuc/index.php?studentprofile=C3836B2C";
        
$question_mark_check = ltrim(stristr($_SERVER["REQUEST_URI"], '?'), '?')
//output: 
studentprofile=C3836B2C
        
        

Part 2: Remove equal sign and everything after it from the previous code executed

            
//set input variable
//variable
$question_mark_check = "studentprofile=C3836B2C"
$equal_sign_check = substr($question_mark_check, strpos($question-mark_check, '=') + 1)
//output: 
studentprofile
        
        

Issues implementing this code

There was one issue encountered when using this code. The code returns an empty value if the url does not have an equal sign or the ampersand in the string. To resolve this an if statement was used.

            

            
        

How this code was used?

            

            
        
References

JS: TITLE

Code Description

--

            
            ---   
            
        

Issues implementing this code

            
            -- 
            
        

How this code was used?

            
        ---
            
        
References