Task - TypeScript - Movies #85
Replies: 14 comments
-
interface Movie {
title: string,
lengthMinutes: number
}
const movies: Movie[] = [{
title: 'American History X',
lengthMinutes: 119,
},
{
title: 'Sherlock Holmes',
lengthMinutes: 128,
},
{
title: 'Scent of a Woman',
lengthMinutes: 157
}];
function compareMovieLengths(x: Movie, y: Movie): number {
if (x.lengthMinutes > y.lengthMinutes) {
return -1;
}
else if (x.lengthMinutes < y.lengthMinutes) {
return 1;
}
else {
return 0;
}
}
console.log(movies.sort(compareMovieLengths));
let longestMovie = movies[0];
console.log(longestMovie);
console.log(longestMovie.title); |
Beta Was this translation helpful? Give feedback.
-
interface Movie {
title: string,
length: number,
}
let movie: Movie[] = [
{
title: 'American History X',
length: 119,
},
{
title: 'Sherlock Holmes',
length: 128,
},
{
title: 'Scent of a Woman',
length: 157
}
];
function compareMovieLengths(x: Movie, y: Movie) {
if (x.length > y.length) {
return -1;
}
if (x.length < y.length) {
return 1;
}
return 0;
}
console.log(compareMovieLengths(movie[0],movie[1]));
var moviesOrdered = movie.sort(compareMovieLengths);
console.log("ordered list is ",moviesOrdered)
console.log("first longest movie is ",moviesOrdered[0])
console.log("longest title is ",moviesOrdered[0].title) |
Beta Was this translation helpful? Give feedback.
-
interface Movies {
title:string,
lengthMinutes:number
}
let movieArray:Array<Movies>=[
{
title: 'American History X',
lengthMinutes: 119
},
{
title: 'Sherlock Holmes',
lengthMinutes: 128,
},
{
title: 'Scent of a Woman',
lengthMinutes: 157
}
];
function compareMovieLengths(movie1:Movies,movie2:Movies):number{
let movie1Length = movie1.title.length;
let movie2Length = movie2.title.length;
if(movie1Length>movie2Length)
return -1;
else if(movie1Length<movie2Length)
return 1;
else
return 0;
}
let longestMovie= movieArray.sort(compareMovieLengths);
console.log(longestMovie[0].title); |
Beta Was this translation helpful? Give feedback.
-
interface Movie {
title: string;
lengthMinutes: number;
}
let Movies: Movie[] = [
{
title: "American History X",
lengthMinutes: 119,
},
{
title: "Sherlock Holmes",
lengthMinutes: 128,
},
{
title: "Scent of a Woman",
lengthMinutes: 157,
},
];
function compareMovieLengths(x: Movie, y: Movie): number {
if (x.lengthMinutes > y.lengthMinutes) return -1;
else if (x.lengthMinutes < y.lengthMinutes) return 1;
else return 0;
}
let longestMovie = Movies.sort(compareMovieLengths);
console.log("Longest Movies is : " + longestMovie[0].title); |
Beta Was this translation helpful? Give feedback.
-
|
Code Output: |
Beta Was this translation helpful? Give feedback.
-
Codeinterface Movie {
title: string,
lengthMinutes: number
}
const movies: Movie[] = [
{
title: 'American History X',
lengthMinutes: 119,
},
{
title: 'Sherlock Holmes',
lengthMinutes: 128,
},
{
title: 'Scent of a Woman',
lengthMinutes: 157
}
];
function compareMovieLengths(x: Movie, y: Movie): number {
if (x.lengthMinutes > y.lengthMinutes) {
return -1;
}
else if (x.lengthMinutes < y.lengthMinutes) {
return 1;
}
else {
return 0;
}
}
console.log(movies.sort(compareMovieLengths));
let longestMovie = movies[0];
console.log(longestMovie);
console.log(longestMovie.title);Output |
Beta Was this translation helpful? Give feedback.
-
interface Movie {
title: string,
length: number,
}
let movie: Movie[] = [
{
title: 'Chak de India',
length: 13,
},
{
title: 'Matribhumi',
length: 115,
},
{
title: 'Bharata',
length: 157
}
];
function compareMovieLengths(x: Movie, y: Movie) {
if (x.length > y.length) {
return -1;
}
else if (x.length < y.length) {
return 1;
}
else{
return 0;
}
}
console.log(compareMovieLengths(movie[0],movie[1]));
var moviesOrdered = movie.sort(compareMovieLengths);
console.log("ordered list is ",moviesOrdered)
console.log("first longest movie is ",moviesOrdered[0])
console.log("longest title is ",moviesOrdered[0].title) |
Beta Was this translation helpful? Give feedback.
-
|
interface Movie { function compareMovieLengths(x: Movie, y: Movie): number { let longestMovie = Movies.sort(compareMovieLengths); |
Beta Was this translation helpful? Give feedback.
-
|
interface Movie { const movies: Movie[] = [ function compareMovieLengths(x: Movie, y: Movie): number { movies.sort(compareMovieLengths); const longestMovie: Movie = movies[0]; console.log( |
Beta Was this translation helpful? Give feedback.
-
code// Movie interface
interface Movie {
title: string;
length: number;
}
// compareMovieLengths function
function compareMovieLengths(x: Movie, y: Movie): number {
if (x.length > y.length) {
return -1;
} else if (x.length < y.length) {
return 1;
} else {
return 0;
}
}
//an array of movie objects
const movies: Movie[] = [
{ title: 'American History X',length: 119},
{ title: 'Scent of a Woman',length: 157},
{ title: 'Sherlock Holmes',length: 128}
];
// Sort the movies array by length using the compareMovieLengths function
movies.sort(compareMovieLengths);
console.log(movies);
// longest movie from the sorted movies array
const longestMovie: Movie = movies[0];
// Print title of the longest movie
console.log(longestMovie.title);
ScreenShot |
Beta Was this translation helpful? Give feedback.
-
/*TypeScript - Movies - Interface, Objects, For Loop
production as optional
Write a function called compareMovieLengths with two Movie parameters x and y and returns one of the following values:
• -1: if the length of Movie x is greater than the length of Movie y
• 1: if the length of Movie x is lesser than the length of Movie y
• else 0
Use this compareMovieLengths function as a comparer to sort the array of movies by their length using the array.sort method.
Get the first element from the ordered array, which is the longest and assign it to a variable called longestMovie.
Print the title of the longestMovie.
*/
//Obkject
interface Movie{
title :string;
lengthMinutes: number;
production?:string; // Otpional Parameter
}
let movies:Movie[]= [
{
title: 'American History X',
lengthMinutes:219,
production: 'USA' // example of structural typing
},
{
title: 'Sherlock Holmes',
lengthMinutes: 198,
},
{
title: 'Scent of a Woman',
lengthMinutes: 157
}
]
function compareMovieLengths(x:Movie,y:Movie):number {
// Ist method
// if(x.lengthMinutes>y.lengthMinutes)
// {
// return -1;
// }
// else if(x.lengthMinutes<y.lengthMinutes)
// {
// return 1;
// }
// else{
// return 0;
// }
// 2nd method
// if(x.lengthMinutes===y.lengthMinutes){
// return 0;
// }
// return x.lengthMinutes>y.lengthMinutes?-1 :1;
// 3rd method - REducing the no fo return
return (Math.sign(y.lengthMinutes-x.lengthMinutes));
}
// console.log(compareMovieLengths(movies[0],movies[1]));
movies.sort(compareMovieLengths);
console.log(movies);
// Calling a function console.log(compareMovieLengths(movies[0],movies[1]));
// longest movie from the sorted movies array
const longMovie: Movie = movies[0];
// Print title of the longest movie
console.log(longMovie.title);screenshot |
Beta Was this translation helpful? Give feedback.
-
CODEinterface Movie2 {
title: string;
lengthMinutes: number;
production: string;
}
const movies: Movie2[] = [
{
title: 'American History X',
lengthMinutes: 119,
production: 'USA'
},
{
title: 'Sherlock Holmes',
lengthMinutes: 128,
production: 'USA'
},
{
title: 'Scent of a Woman',
lengthMinutes: 157,
production: 'USA'
}
];
function compareMovieLengths(x: Movie2, y: Movie2): number {
if (x.lengthMinutes > y.lengthMinutes) {
return -1;
} else if (x.lengthMinutes < y.lengthMinutes) {
return 1;
} else {
return 0;
}
}
movies.sort(compareMovieLengths);
const longestMovie: Movie2 = movies[0];
console.log(`The longest movie is "${longestMovie.title}"`);
OUTPUT |
Beta Was this translation helpful? Give feedback.
-
codeinterface MovieTs{
title: string;
lengthMinutes: number;
production?: string;
}
let movies: MovieTs[] = [
{
title: 'American History X',
lengthMinutes: 119,
production: 'USA' // example of structural typing
},
{
title: 'Sherlock Holmes',
lengthMinutes: 128
},
{
title: 'Scent of a Woman',
lengthMinutes: 157
}
]
let moviesXindex = 0;
let moviesYindex = 0;
/*
function compareMovieLengths(x: string, y: string ){
for(let i = 0; i < movies.length; i++) {
if(x == movies[i].title){
moviesXindex = i;
}
if(y == movies[i].title){
moviesYindex = i;
}
}
}
if(moviesXindex>moviesYindex){
console.log('-1')
}
else{
console.log('1')
}
*/
function compareMovieLengths(x: MovieTs, y: MovieTs ): number{
if(x.lengthMinutes > y.lengthMinutes){
return -1;
}
else if((x.lengthMinutes < y.lengthMinutes)){
return 1;
}
else{
return 0;
}
}
movies.sort(compareMovieLengths);
console.log(movies)output |
Beta Was this translation helpful? Give feedback.
-
|
Code const Movies: Array function compare_movie_lengths(x:Movie, y:Movie) { } Movies.sort(compare_movie_lengths); const longestMovie = Movies[0]; Output |
Beta Was this translation helpful? Give feedback.




Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Create a typescript interface called
Moviewhich defines two attributes:title:stringlengthMinutes:numberCreate an array called
moviesthat is typed using theMovieinterface.Push the following movies into the
moviesarray:Write a function called
compareMovieLengthswith twoMovieparametersxandyand returns one of the following values:Moviexis greater than the length ofMovieyMoviexis lesser than the length ofMovieyUse this
compareMovieLengthsfunction as a comparer to sort the array of movies by their length using thearray.sortmethod.Get the first element from the ordered array, which is the longest and assign it to a variable called
longestMovie.Print the title of the
longestMovie.Beta Was this translation helpful? Give feedback.
All reactions