var array = [1, 2, 3, 4, 5];
// made an array
for (var i = 0; array[i]; i++) // browse your array with this simple condition on index
console.log('element '+i+' = '+array[i]);
//output:
element 0 = 1
element 1 = 2
element 2 = 3
element 3 = 4
element 4 = 5
// Find-Max variant -- rather than finding the max value, find the
// *index* of the max value
public int findMaxIndex(int[] nums) {
int maxIndex = 0; // say the 0th element is the biggest
int maxValue = nums[0];
// Look at every element, starting at 1
for (int i=1; i<nums.length; i++) {
if (nums[i] > maxValue) {
maxIndex = i;
maxValue = nums[maxIndex];
}
}
return maxIndex;
}
var array = ['a', 'b', 'c']
array.forEach((value, index) => {
console.log(index); // Will log each index
console.log(value); // Will log each value
});
int findSmallest(int[] values) {
int minIndex = 0; // start with 0th element as min
for (int i=1; i<values.length; i++) {
if (values[i] < values[minIndex]) {
minIndex = i;
}
}
return minIndex;
}
/**
* !What is Hoisting --------------->
* *Hoisting is a phenomena where we can access the variables and functions before initialization
*/
myFunc("Jassi"); // my name is Jassi
console.log(a); // undefined
var a = 5;
function myFunction(a) {
var b = `my name is ${a}`;
console.log(b);
}
// Given an array of booleans representing a series
// of coin tosses (true=heads, false=tails),
// returns true if the array contains anywhere within it
// a string of 10 heads in a row.
// (example of a search loop)
public boolean searchHeads(boolean[] heads) {
int streak = 0; // count the streak of heads in a row
for (int i=0; i<heads.length; i++) {
if (heads[i]) { // heads : increment streak
streak++;
if (streak == 10) {
return true; // found it!
}
}
else { // tails : streak is broken
streak = 0;
}
}
// If we get here, there was no streak of 10
return false;
}
// Find-Max variant
// Use very small number, Integer.MIN_VALUE,
// as the initial max value.
public int findMax2(int[] nums) {
int maxSoFar = Integer.MIN_VALUE; // about -2 billion
// Look at every element
for (int i=0; i<nums.length; i++) {
if (nums[i] > maxSoFar) {
maxSoFar = nums[i];
}
}
return maxSoFar;
}
// Find-Max
// Given a non-empty array of ints, returns
// the largest int value found in the array.
// (does not work with empty arrays)
public int findMax(int[] nums) {
int maxSoFar = nums[0]; // use nums[0] as the max to start
// Look at every element, starting at 1
for (int i=1; i<nums.length; i++) {
if (nums[i] > maxSoFar) {
maxSoFar = nums[i];
}
}
return maxSoFar;
}
// Another way to write search that combines
// the "end of array" and "found" logic in one
// while loop. As a matter of style, we prefer
// the above version that uses the standard
// for-all loop.
public int searchNotAsGood(int[] nums, int target) {
int i = 0;
while (i<nums.length && nums[i]!=target) {
i++;
}
// get here either because we found it, or hit end of array
if (i==nums.length) {
return -1;
}
else {
return i;
}
}
// Search
// Searches through the given array looking
// for the given target. Returns the index number
// of the target if found, or -1 otherwise.
// (classic search-loop example)
public int search(int[] nums, int target) {
// Look at every element
for (int i=0; i<nums.length; i++) {
if (nums[i] == target) {
return i; // return the index where the target is found
}
}
// If we get here, the target was not in the array
return -1;
}
// For-All variant
// Go through the elements backwards
// by adjusting the for-loop
public void forAllBackwards(int[] nums) {
for (int i = nums.length-1; i>=0; i--) {
System.out.println( nums[i] );
}
}
Account[] accounts;
// 1. allocate the array (initially all the pointers are null)
accounts = new Account[100];
// 2. allocate 100 Account objects, and store their pointers in the array
for (int i=0; i<accounts.length; i++) {
accounts[i] = new Account();
}
// 3. call a method on one of the accounts in the array
account[0].deposit(100);