Upload files to "/"
This commit is contained in:
27
StockBehavior.js
Normal file
27
StockBehavior.js
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, 5,5,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){
|
||||
let finishedarray = [];
|
||||
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))
|
||||
56
StringChanllenge.js
Normal file
56
StringChanllenge.js
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
QUOTE: I have a dream that my four little children will one day live
|
||||
in a nation where they will not be judged by the color of
|
||||
their skin but by the content of their character.
|
||||
*/
|
||||
let meow = "I have a dream that my four little children will one day live in a nation where they will not be judged by the color of their skin but by the content of their character."
|
||||
// Function getFrequency takes as parameters two strings, text and token.
|
||||
// This function needs to return the number of times a token accurs in text
|
||||
// For example, if this function is tested with above quote and token is
|
||||
// "will", then this function should return 2 becuase "will" occurs twice in
|
||||
// the text
|
||||
function getFrequency(text, token) {
|
||||
let count = 0;
|
||||
let words = text.split(" "); // Changed from text.split("")
|
||||
for (let i=0; i<words.length; i++) {
|
||||
let word=words[i]
|
||||
if (word === token) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
console.log(getFrequency(meow, "will"));
|
||||
|
||||
// Function search takes as parameters two strings, text and token.
|
||||
// This function needs to return the location where token occurs
|
||||
// in parameter text. For example, if you call this function with
|
||||
// token = "dream", this function will return 9. If token does not exist
|
||||
// in text this function should return -1
|
||||
function search(text, token){
|
||||
return text.indexOf(token)
|
||||
}
|
||||
|
||||
console.log(search(meow, "dream"))
|
||||
|
||||
|
||||
// Function spellWord takes as parameters two strings, text and word.
|
||||
// This function needs to return a spell of parameter word. For example,
|
||||
// if you call this function with token = "children", this function should
|
||||
// return c,h,i,l,d,r,e,n
|
||||
// If word does not exist in text, this function should return "Word not found"
|
||||
function spellWord(text, word) {
|
||||
const words = text.split(" ").filter(s => s.length > 0);
|
||||
const lowercaseWords = words.map(w => w.toLowerCase());
|
||||
const lowerCaseWord = word.toLowerCase();
|
||||
|
||||
if (lowercaseWords.includes(lowerCaseWord)) {
|
||||
return word.split("").join(",");
|
||||
} else {
|
||||
return "Word not found";
|
||||
}
|
||||
}
|
||||
|
||||
console.log(spellWord(meow, "children"))
|
||||
|
||||
// write code to test your functions
|
||||
53
problem1.js
Normal file
53
problem1.js
Normal file
@@ -0,0 +1,53 @@
|
||||
|
||||
|
||||
// Function computeAverage() is complete. This function
|
||||
// takes an array of test named scores and two integers, from and to.
|
||||
// computeScores returns the average of all the scores starting at
|
||||
// from and ending at to, inclusive
|
||||
function computeScores(scores, from, to)
|
||||
{
|
||||
//DO NOT WRITE ANY CODE HERE
|
||||
let sum = 0;
|
||||
let average =0;
|
||||
for(let i=from; i<=to; i++)
|
||||
{
|
||||
sum = sum + scores[i]
|
||||
}
|
||||
|
||||
average = sum/(to-from+1)
|
||||
return average
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Function getRange() is complete. This function
|
||||
// takes an array of test scores and two integers, from and to.
|
||||
// getRange returns a string consisting of all the scores from scores
|
||||
// starting at from and ending at to, inclusive separated by a space
|
||||
function getRange(scores, from, to)
|
||||
{
|
||||
//DO NOT WRITE ANY CODE HERE
|
||||
let s="";
|
||||
for(let i=from; i<=to; i++)
|
||||
{
|
||||
s = s + scores[i] +" "
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// ======= Your task is specified in the WORD DOCUMENT ================
|
||||
|
||||
|
||||
let scores = [95, 100,80, 80, 100, 80, 60, 80, 90]
|
||||
let scores2 = [95, 60, 80, 80, 50, 80, 60, 80, 90]
|
||||
let scores3 = [95, 100, 75, 80, 80, 80, 60, 80, 75]
|
||||
|
||||
console.log(`Scores: ${getRange(scores, 2, 5)}`)
|
||||
console.log(`Average:${computeScores(scores, 2, 5)}`)
|
||||
console.log()
|
||||
console.log(`Scores: ${getRange(scores2, 4, 8)}`)
|
||||
console.log(`Average:${computeScores(scores2, 4, 8)}`)
|
||||
console.log()
|
||||
console.log(`Scores: ${getRange(scores3, 0, 8)}`)
|
||||
console.log(`Average:${computeScores(scores3, 0, 8)}`)
|
||||
101
problem2.js
Normal file
101
problem2.js
Normal file
@@ -0,0 +1,101 @@
|
||||
//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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
//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, start, end)
|
||||
{
|
||||
//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)
|
||||
{
|
||||
if (hasImproved(scores)) {
|
||||
let temparray = scores.slice(Math.ceil(scores.length / 2), scores.length)
|
||||
return getAverage(temparray, 0, temparray.length-1)
|
||||
|
||||
} else {
|
||||
// console.log("nuh uh")
|
||||
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 = [85, 90, 96, 97, 98, 100]
|
||||
let array2 = [95, 60, 100, 80, 80, 85, 87, 90]
|
||||
let array3 = [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)}`)
|
||||
|
||||
20
sort.js
Normal file
20
sort.js
Normal file
@@ -0,0 +1,20 @@
|
||||
let sort = [5, 10, -6, 4, 3, 2, -7, 20, 8, 9]
|
||||
|
||||
function sortList(list) {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
console.log(sortList(sort))
|
||||
Reference in New Issue
Block a user