DEV Community

Jingwen Xu
Jingwen Xu

Posted on

Daily learning log

basic JavaScript learning


Basic JavaScript: Shopping ListPassed

Create a shopping list in the variable myList. The list should be a multi-dimensional array containing several sub-arrays.

The first element in each sub-array should contain a string with the name of the item. The second element should be a number representing the quantity i.e.

["Chocolate Bar", 15]

There should be at least 5 sub-arrays in the list.

var myList = [
    ["chocolate bar",15],
    ["yogurt",3],
    ["soya beans",1],
    ["pretzel",5],
    ["ananas",2],
];
Enter fullscreen mode Exit fullscreen mode

Basic JavaScript: Passing Values to Functions with ArgumentsPassed

Parameters are variables that act as placeholders for the values that are to be input to a function when it is called. When a function is defined, it is typically defined along with one or more parameters. The actual values that are input (or "passed") into a function when it is called are known as arguments.

Here is a function with two parameters, param1 and param2:

function testFun(param1, param2) {
console.log(param1, param2);
}
Then we can call testFun: testFun("Hello", "World"); We have passed two arguments, "Hello" and "World". Inside the function, param1 will equal "Hello" and param2 will equal "World". Note that you could call testFun again with different arguments and the parameters would take on the value of the new arguments.

Create a function called functionWithArgs that accepts two arguments and outputs their sum to the dev console.
Call the function with two numbers as arguments.

function functionWithArgs(a, b) {
    console.log(a + b)
}

functionWithArgs(1,2);
functionWithArgs(7,9);
Enter fullscreen mode Exit fullscreen mode

Basic JavaScript: Stand in Line

In Computer Science a queue is an abstract Data Structure where items are kept in order. New items can be added at the back of the queue and old items are taken off from the front of the queue.

Write a function nextInLine which takes an array (arr) and a number (item) as arguments.

Add the number to the end of the array, then remove the first element of the array.

The nextInLine function should then return the element that was removed.

// Setup
var testArr = [1,2,3,4,5];

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));

nextInLine([], 5);
nextInLine([], 1) ;
nextInLine(testArr, 10)
Enter fullscreen mode Exit fullscreen mode

Basic JavaScript: Assignment with a Returned ValuePassed

If you'll recall from our discussion of Storing Values with the Assignment Operator, everything to the right of the equal sign is resolved before the value is assigned. This means we can take the return value of a function and assign it to a variable.

Assume we have pre-defined a function sum which adds two numbers together, then:

ourSum = sum(5, 12);

will call sum function, which returns a value of 17 and assigns it to ourSum variable.

Call the processArg function with an argument of 7 and assign its return value to the variable processed.

// Setup
var processed = 0;

function processArg(num) {
  return (num + 3) / 5;
}

// Only change code below this line
processed = processArg(7);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)