Upload files to "/"
This commit is contained in:
27
StockBehavior.ts
Normal file
27
StockBehavior.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
//The array stocks stores the behavior of stocks for one year.
|
||||||
|
//The numbers in the array represent percentages.
|
||||||
|
|
||||||
|
let stocks = [ 5, 6, 3, 4, 5, 8, 5, 3, 2, 10, 9, 5]
|
||||||
|
|
||||||
|
|
||||||
|
//A peak is when the percentage on the left of a given month M is less and the
|
||||||
|
//percentage on the right of month M is greater. For example, the list given above, contains
|
||||||
|
//three peaks, they are 6, 8, and 10 (positions 1, 5 and 9 respectively)
|
||||||
|
// Complete function getPeaks so that it returns an array containing all the locations
|
||||||
|
// of all peaks that a exists in the parameter array named list.
|
||||||
|
// If there are no peaks, this function should return an empty array ([])
|
||||||
|
function getPeaks(list: number[]){
|
||||||
|
let finishedarray: number[] = [];
|
||||||
|
for (let i = 0; i < list.length-1; i++) {
|
||||||
|
// console.log(`i is ${i}, arrindex is ${list[i]}, before, ${list[i-1]}, after ${list[i+1]}`)
|
||||||
|
if (list[i]! > list[i - 1]! && list[i]! > list[i+1]!) {
|
||||||
|
finishedarray.push(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return finishedarray;
|
||||||
|
}
|
||||||
|
|
||||||
|
// for the array stocks shown above, getPeaks should return [1, 5, 9]
|
||||||
|
// WRITE CODE TO TEST YOUR FUNCTION HERE.
|
||||||
|
|
||||||
|
console.log(getPeaks(stocks))
|
||||||
15
StringChallenge.ts
Normal file
15
StringChallenge.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
function fizzBuzz(n: number): string[] {
|
||||||
|
let h = []
|
||||||
|
for (let i=1; i<=n;i++) {
|
||||||
|
let string = ''
|
||||||
|
if (i % 3 === 0){
|
||||||
|
string += 'Fizz'
|
||||||
|
} else if (i % 5 === 0) {
|
||||||
|
string += 'Buzz'
|
||||||
|
} else if (string === '') {
|
||||||
|
string += i
|
||||||
|
}
|
||||||
|
h.push(string)
|
||||||
|
}
|
||||||
|
return h
|
||||||
|
};
|
||||||
97
problem2.ts
Normal file
97
problem2.ts
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
//Function displayList is complete
|
||||||
|
//Function displayList returns a string (a list of) of all test scores
|
||||||
|
//stored in the parameter array named scores
|
||||||
|
function displayList(scores: number[]): string
|
||||||
|
{
|
||||||
|
let s = ""
|
||||||
|
for(let loc=0; loc<scores.length; loc++)
|
||||||
|
{
|
||||||
|
if(loc<scores.length-1)
|
||||||
|
s = s+ scores[loc] +", "
|
||||||
|
else
|
||||||
|
s = s+ scores[loc]
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// function hasImproved() takes as parameter an array of scores named scores.
|
||||||
|
// This function returns TRUE if all scores are improving, returns FALSE otherwise.
|
||||||
|
// scores are said to be improving if all scores in the list are greater than the
|
||||||
|
// previous score. For example [50, 80, 90, 100] is improving since any given score
|
||||||
|
// in the list is greater than the previous. On the otherhand, scores [90, 95, 85, 100]
|
||||||
|
// are not improving because 85<95
|
||||||
|
function hasImproved(scores: number[]): boolean
|
||||||
|
{
|
||||||
|
//DO NOT WRITE ANY CODE HERE
|
||||||
|
for(let i=0; i<scores.length-1; i++)//5 6 8 9 4
|
||||||
|
{
|
||||||
|
for(let j=i+1; j<scores.length-2; j++)
|
||||||
|
{
|
||||||
|
if(scores[j]!<scores[i]!)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// function getAverage is complete.
|
||||||
|
// This function takes an array of test scores and two integers, start and end.
|
||||||
|
// getAverage returns the average of all scores starting at start and end
|
||||||
|
// at to inclusive. For example. if scores is [80, 85, 75, 90, 100, 50] and
|
||||||
|
// start=2 and end=4, getAverage will return the average of 75, 90 and 100
|
||||||
|
function getAverage(scores: number[], start:number, end: number): number
|
||||||
|
{
|
||||||
|
//DO NOT WRITE ANY CODE HERE
|
||||||
|
let sum=0;
|
||||||
|
for(let i=start; i<=end; i++)
|
||||||
|
{
|
||||||
|
sum = sum + scores[i]!
|
||||||
|
}
|
||||||
|
return sum/(end-start+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ******************************** TASK 1 ***********************************
|
||||||
|
// Function computeGrade() takes as parameters an array of student test scores
|
||||||
|
// named scores.
|
||||||
|
// This function needs to return the average of this student. The average
|
||||||
|
// needs to be computed according to the following rules:
|
||||||
|
// 1. If the student has improve, the average is computed from the scores on the
|
||||||
|
// second half of the list ONLY. If the list has odd number of scores, the extra score
|
||||||
|
// is part of the second half. For example, if the student test scores list is
|
||||||
|
// [50, 80, 85, 90, 95] the average should be calculated with scores [85, 90, 95] use LabArrays for this, and use improve function for boolean :3
|
||||||
|
// 2. If the student has not improved, the average score is calculated including
|
||||||
|
// all the test scores
|
||||||
|
function computeGrade(scores: number[])
|
||||||
|
{
|
||||||
|
if (hasImproved(scores)) {
|
||||||
|
let temparray: number[] = scores.slice(Math.ceil(scores.length / 2), scores.length)
|
||||||
|
return getAverage(temparray, 0, temparray.length-1)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return getAverage(scores, 0, scores.length-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ******************************** TASK 2 ***********************************
|
||||||
|
// Write the code to calculate the average of the following test scores and
|
||||||
|
// display the data as shown in the sample run of the program shown in the
|
||||||
|
// WORD DOCUMENT.
|
||||||
|
// [95, 100, 80, 80, 100, 80, 60, 80, 90]
|
||||||
|
// [95, 60, 80, 80, 50, 80, 60, 80, 90]
|
||||||
|
// [95, 100, 75, 80, 80, 80, 60, 80, 75]
|
||||||
|
// You MUST use the other given functions when completing this TASK
|
||||||
|
let array: number[] = [85, 90, 96, 97, 98, 100]
|
||||||
|
let array2: number[] = [95, 60, 100, 80, 80, 85, 87, 90]
|
||||||
|
let array3: number[] = [95, 100, 75, 80, 80, 80, 60, 80, 75]
|
||||||
|
|
||||||
|
console.log(`Test Scores: ${displayList(array)}`)
|
||||||
|
console.log(`Average = ${computeGrade(array)}`)
|
||||||
|
console.log()
|
||||||
|
console.log(`Test Scores: ${displayList(array2)}`)
|
||||||
|
console.log(`Average = ${computeGrade(array2)}`)
|
||||||
|
console.log()
|
||||||
|
console.log(`Test Scores: ${displayList(array3)}`)
|
||||||
|
console.log(`Average = ${computeGrade(array3)}`)
|
||||||
|
|
||||||
|
|
||||||
24
sort.ts
Normal file
24
sort.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
let sort = [5, 10, -6, 4, 3, 2, -7, 20, 8, 9]
|
||||||
|
|
||||||
|
function sortList(list: number[]) {
|
||||||
|
for(let i=0; i< list.length-1; i++) {
|
||||||
|
let pos = i // o
|
||||||
|
for (let j=i+0; i<list.length-1; j++) {
|
||||||
|
if (list[j]<list[pos]) {
|
||||||
|
pos = j
|
||||||
|
}
|
||||||
|
if (pos !== i) {
|
||||||
|
let temp = list[i];
|
||||||
|
list[i] = list[j];
|
||||||
|
list[j] = temp; // Corrected line
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pos
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(sortList(sort))
|
||||||
|
|
||||||
|
sort.toLocaleString
|
||||||
67
typescriptisbetter.ts
Normal file
67
typescriptisbetter.ts
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
// humberto mundo
|
||||||
|
let animals: string[] = ["Zebra", "Koala", "Lion", "Elephant", "Eagle", "Cat", "Eagle"]
|
||||||
|
|
||||||
|
// Write a function named getLocation(list, word) to return the location of the parameter word
|
||||||
|
// in the array list, if it exist, returns -1 if it does not exists
|
||||||
|
|
||||||
|
function getLocation(list: string[], word: string): number {
|
||||||
|
return list.indexOf(word)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Write a function named getLongest(list) which needs to return the longest string in array
|
||||||
|
// parameter array
|
||||||
|
|
||||||
|
function getLongest(array: string[] ): string {
|
||||||
|
if (!array) return "provide a array";
|
||||||
|
|
||||||
|
let long = array[0]; // init w/ first element
|
||||||
|
for (let i = 1; i < array.length; i++) {
|
||||||
|
// const element = list[i];
|
||||||
|
if (array[i]!.length > long!.length) { // this is why i love and hate typescript :3
|
||||||
|
long = array[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return long as string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// extra cus y not
|
||||||
|
|
||||||
|
function getShortest(list: string[]): string {
|
||||||
|
if (!list) return "i need array >:("
|
||||||
|
|
||||||
|
let short = list[0]
|
||||||
|
|
||||||
|
for (let i = 1; i < list.length; i++) {
|
||||||
|
if (list[i]!.length < short!.length) {
|
||||||
|
short = list[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return short as string;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Write a function named spellBackward(list, word) which it needs to return parameter word
|
||||||
|
// spell backwards if it exist in array list, otherwise it return word does not exist.
|
||||||
|
|
||||||
|
function spellBackward(list: string[], word: string): string {
|
||||||
|
if (!list.includes(word)) return "word does not exist";
|
||||||
|
return word.split("").reverse().join("");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//write the rest of the code here
|
||||||
|
|
||||||
|
console.log(getLocation(animals, "Eagle")) // uhhhh 4
|
||||||
|
console.log(getLocation(animals, "cat")) // should return -1 since its Cat not cat
|
||||||
|
|
||||||
|
console.log() // spacers
|
||||||
|
|
||||||
|
console.log(getLongest(animals)) // Elephant
|
||||||
|
console.log(getShortest(animals)) // Cat
|
||||||
|
|
||||||
|
console.log() // spacers
|
||||||
|
|
||||||
|
// i am the best programmer in the world
|
||||||
|
console.log(spellBackward(animals, "Eagle")) // should return elgaE
|
||||||
|
console.log(spellBackward(animals, "cat")) // should return "word does not exist"
|
||||||
Reference in New Issue
Block a user