Problem Solving with JavaScript


Here's the function to add numbers: 

function addNumbers(num1, num2) {
return num1 + num2;
}


 Here's the function to convert minutes to seconds: 

function convertMinutesToSeconds(minutes) {
return minutes * 60;
}

 Here's the function to calculate your age in seconds: 

function yourAgeInSeconds(age) {
return age * 12 * 30 * 24 * 60 * 60;
}

 Here's the function to calculate your age in seconds: 

function yourAgeInSeconds(age) {
return age * 12 * 30 * 24 * 60 * 60;
}

To get the first item of a list.


Deciding if a movie is good or bad:
let movie_result;

function badOrGoodMovie(movie_rating) {
if (movie_rating >= 7) {
movie_result = 'Good movie'
} else {
movie_result = 'Bad movie'
}
return movie_result;
}

Check an empty string:

//console.log(badOrGoodMovie(6));

function isEmptyString(some_string) {
if (some_string == "") {
return true;
} else {
return false;
}
}

Find the minimum:

function findMin(numbers) {
let minimum = numbers[0];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] < minimum) {
minimum = numbers[i];
}
}
return minimum;
}

Find the maximum:

function findMax(numbers) {
let maximum = numbers[0];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] > maximum) {
maximum = numbers[i];
}
}
return maximum;
}

Find the maximum with the location, returning a dictionary:

function findMaxHelper(numbers, start) {
let maximum = numbers[start];
let max_location = start

for (let i = start; i < numbers.length; i++) {
if (numbers[i] > maximum) {
maximum = numbers[i]
max_location = i
}
}
return { max_number: maximum, max_index: max_location }
}

Sorting with the indexes sorted as well.

function sortBestWithHelper(numbers) {
// run as many times as there are items
for (let j = 0; j < numbers.length - 1; j++) {
// Find max number and max location starting from j
max = findMaxHelper(numbers, j)
max_num = max['max_number']
max_location = max['max_index']
// swap the first and max item in an array
numbers[max_location] = numbers[j] //
numbers[j] = max_num
}
return numbers
}



Getting the movies as a JSON index:
let movies = [
{
title: "Fight Club",
rank: 10,
id: "tt0137523"
},
{
title: "The Shawshank Redemption",
rank: 1,
id: "tt0111161"
},
{
title: "The Lord of the Rings: The Return of the King",
rank: 9,
id: "tt0167260"
},
{
title: "The Godfather",
rank: 2,
id: "tt0068646"
},
{
title: "The Good, the Bad and the Ugly",
rank: 5,
id: "tt0060196"
},
{
title: "The Godfather: Part II",
rank: 3,
id: "tt0071562"
},
{
title: "The Dark Knight",
rank: 6,
id: "tt0468569"
},
{
title: "Pulp Fiction",
rank: 4,
id: "tt0110912"
},
{
title: "Schindler's List",
rank: 8,
id: "tt0108052"
},
{
title: "12 Angry Men",
rank: 7,
id: "tt0050083"
}
]

Then, we have a function that denotes what to do when we load a function from the window:

window.onload = function () {
let sortedMovieList = sortMoviesByAttr(movies, 'rank');
// Display Movies list
displayMovies(sortedMovieList);

let sortedMovieList2 = sortMoviesByAttr(movies, 'title');
// Display Movies list
displayMovies(sortedMovieList2);
}

Now, we can display this by accessing dictionary elements and by displaying <tr> and <td> column elements in the row:
function displayMovies(movies) {
let table = "<table border = '1' style = 'width: 100%'>";
table += "<tr><th>ID</th><th>Name</th>Rank</th></tr>";
for (let index = 0; index < movies.length; index++) {
table += "<tr>";
table += "<td>" + movies[index].id + "</td>";
table += "<td>" + movies[index].title + "</td>";
table += "<td>" + movies[index].rank + "</td>";
table += "</tr>";
}
table += "</table>";
document.getElementById("movies-list").innerHTML = table;
}



Now, we can sort movies by rank:
function sortMoviesByRank(numbers) {
for (let j = 0; j < numbers.length - 1; j++) {
let max_num = numbers[j];
let max_location = j;
for (let i = j; i < numbers.length; i++) {
if (numbers[i] > max_num) {
max_num = numbers[i];
max_location = i;
}
}
numbers[max_location] = numbers[j];
numbers[j] = max_num;
}
return numbers;
}

Then, we sort movies by attribute, we do this by looking at the variable called sortattr.

function sortMoviesByAttr(movies, sortAttr) {
for (let j = 0; j < movies.length - 1; j++) {
let max_obj = movies[j];
let max_location = j;
for (let i = j; i < movies.length; i++) {
if (movies[i][sortAttr] > max_obj[sortAttr]) {
max_obj = movies[i];
max_location = i;
}
}
movies[max_location] = movies[j];
movies[j] = max_obj;
}
return movies;
}

To get the max movie object, we need to look at the maxlocation and object pointer number, and then update the location and objects accordingly.

function getMaxMovieObject(movies, start, sortAttr) {
let maximum = numbers[start];
let max_location = start;
for (let i = start; i < numbers.length; i++) {
if (numbers[i] > maximum) {
maximum = numbers[i];
max_location = i;
}
}
return {max_number: maximum, max_index: max_location}

}


And here's the html that goes along:

<!DOCTYPE html>
<html>

<head>
<meta charset = "utf-8" />
<meta name= "viewport" content= "width = device-width">
<title>repl.it</title>
<link href= "style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<script src="script.js"></script>
<div id = "movies-list">


</div>
</body>
</html >

Comments

Popular Posts