//Examples
Math.PI
Math.random()
"aeiou".length
"aeiou".charAt(2)
"aeiou".charAt(2).toUpperCase()
let vowels = "aeiou";
//traverse string from 0 to length - 1
for (let i = 0; i < vowels.length; i++) {
alert(vowels[i]); //array subscript operator
}
//recognizer for vowels
var isVowel = function(ch){
//https://www.kirupa.com/html5/the_devowelizer.htm
};
//count the vowels in a sentence
var countVowels = function(s){
//initialize a counter
var count = 0;
//traverse string
for(var i = 0; i < s.length; ++i){
if(isVowel(s.charAt(i)) //array subscript operator
++count;
}
return count;
};
//recognizer for vowels
var isVowel = function(ch){
//https://www.kirupa.com/html5/the_devowelizer.htm
};
//return all the vowels in a string
let getVowels = function(s){
let result = "";
//traverse the string
for(let i = 0; i < s.length; ++i){
if(isVowel(s[i])
result = result + s[i];
}
return result;
};
Write and test a function named countOccurrences that accepts two arguments (a string and a character) and counts the number of times the character appears in the string.
countOccurrences(“Alpha”, “a”) => 2
let countOccurrences = function(s, ch){
//initialize a counter
let count = 0;
//traverse the string
for(let i = 0; i < s.length; ++i){
if(the current char in s is equal to ch)
add 1 to count
}
return count;
};
Examples:
var groceries = ["Milk", "Eggs", "FrostedFlakes", "Salami", "Juice"];
var result = "";
var item;
for (var i = 0; i < groceries.length; ++i) {
item = groceries[i];
result = result + " " + item.toUpperCase();
}
console.log(result); // MILK EGGS FROSTEDFLAKES SALAMI JUICE
let primes = [2, 3, 5, 7]; console.log(primes[0]); // 2 console.log(primes[0] + 1); // 3 console.log(primes[0] + primes[1]); // 5 console.log(primes[primes[1]); // 7
let sumArr = function(arr){
let sum = 0;
//sum all numbers in array
for(let i = 0; i < arr.length; ++i){
sum += arr[i];
}
return sum;
};
let groceries = ["Milk", "Eggs", "FrostedFlakes"];
groceries[3] = "Spam";
console.log(groceries); // ["Milk", "Eggs", "FrostedFlakes", "Spam"]
// -> inspect the array in the console
let groceries = ["Milk", "Eggs", "FrostedFlakes"];
groceries.push("Spam");
console.log(groceries); // ["Milk", "Eggs", "FrostedFlakes", "Spam"]
let groceries = ["Milk", "Eggs", "FrostedFlakes", "Spam"]; let e; //remove element from end e = groceries.pop(); console.log(e); // "Spam" console.log(groceries); // ["Milk", "Eggs", "FrostedFlakes"]
let groceries = ["Milk", "Eggs", "FrostedFlakes", "Spam"]; let e; //remove initial element //delete, and shift left e = groceries.shift(); console.log(e); // "Milk" console.log(groceries); // ["Eggs", "FrostedFlakes", "Spam"]
let groceries = ["Eggs", "FrostedFlakes", "Spam"];
let e;
//add new element at front
//shift right, and insert
groceries.unshift("Milk");
console.log(groceries); // ["Milk", "Eggs", "FrostedFlakes", "Spam"]
let groceries = ["Milk", "Eggs", "FrostedFlakes", "Spam"];
let pos = groceries.indexOf('Spam');
console.log(pos); // 3
pos = groceries.indexOf('FruitLoops');
console.log(pos); // -1
let getPrimes = function(n){
let primes = [];
for(let i = 2; i <= n; ++i){
if(isPrime(i))
primes[i] = i;
}
return primes;
};
let primesArr = getPrimes(10);
console.log(primesArr); // [empty, empty, 2, 3, empty, 5, empty, 7]
36 possible combinations:
Probabilities of rolling N:
(ways of rolling N / 36)
let rollDie = function() {
return Math.floor(Math.random() * 6) + 1;
};
let die1 = rollDie();
let die2 = rollDie();
let num = die1 + die2;
console.log(num);
Q: How should we declare the array to hold rolls of 2 .. 12?
let rollDice = function(n) {
//array to hold dice rolls. 1st two elements not used (rolls of 0 and 1)
let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
//let arr = new Array(13).fill(0);
let die1, die2, num;
//roll dice n times
for (let i = 1; i <= n; ++i) {
die1 = rollDie();
die2 = rollDie();
num = die1 + die2;
++arr[num]; //add 1 to the times we rolled num
}
return arr;
};
HTML, CSS & JavaScript are the three pillars of Web Standards.
http://blogs.uoregon.edu/cis11schedule/