Back to Basic JavaScript?

Ok, so FreeCodeCamp has added some new stuff, including a lot of new Basic JavaScript projects. I’m going back to that section and thought I’d post a few of my solutions here…

Return a Value from a Function with Return

function timesFive(n) {
return n * 5;
}
timesFive(5); // returns 25
timesFive(10); // returns 50

Assignment with a Returned Value

var processed = 0;

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

processed = process(7); // returns a value of 2

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 queue 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 array. The queue function should then return the element that was removed.”

function queue(arr, item) {
arr.push(item);
return arr.shift();
}

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

// Display Code
console.log(“Before: ” + JSON.stringify(testArr));
console.log(queue(testArr, 6)); // Modify this line to test
console.log(“After: ” + JSON.stringify(testArr));

So I completely forget about using push() and had to use some help on this one…

Use Conditional Logic with If Statements

function myFunction(wasThatTrue) {
if (wasThatTrue) {
return “That was true”;
}
return “That was false”;
}

myFunction(true); // returns “That was true”

And I’m done for tonight! Tomorrow’s post will be about Comparison Operators.

Leave a comment