Practice Coding Answers

1) 

Possible Solution 1:

sum = 0;

for (i = 0; i < numbers.length; i++)

{

  sum = sum + numbers[i];

}


Possible Solution 2: 

sum = 0;

index = 0;

while (index < numbers.length)

{

  sum += numbers[index];

  index = index + 1;

}


2) 

Possible Solution 1:

totalPay= hoursWorked*payRate;

if (hoursWorked>40)

{

totalPay = 40*payRate + (hoursWorked-40)*payRate*1.5;

}


Possible Solution 2:

totalPay= hoursWorked*payRate;

if (hoursWorked>40)

{

totalPay  += (hoursWorked-40)*(payRate/2);

}


3) 

Possible Solution 1:

right = 0;

numQuestions = 0;

for (i =0;i < choices.length; i++)

{

if (choices[i]==correct[i]) right++;

numQuestions++;

}

grade  = right/numQuestions;


Possible Solution 2:

grade = 0;

for (i =0;i < choices.length; i++)

{

if (choices[i]==correct[i]) grade++;

}

grade  /= choices.length;


4)

Possible Solution 1:

range = data[data.length-1] - data[0]


Possible Solution 2:

firstIndex = 0;

lastIndex = data.length-1;

low = data[firstIndex];

high = data[lastIndex];

range = high - low;


5) 

Possible Solution 1:

max = data[0];

for (i = 1; i < data.length; i++)

{

 if (data[i]>max) max = data[i];

}


Possible Solution 2:

max = data[0];

for (i = 1; i < data.length; i++)

{

 current = data[i];

if (current>max) max =current;

}