Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ARRAYS AGAIN OBJECTIVE Practice again with arrays. BACKGROUND Your company sells

ID: 3598746 • Letter: A

Question

ARRAYS AGAIN

OBJECTIVE

Practice again with arrays.

BACKGROUND

Your company sells products to customers. For each product the insurance is free up to a certain product cost.

Product cost above that maximum is the basis for insurance cost.

Calculate the total cost of insurance for a list of products sold.

NOTICE

These instructions are deliberately brief. The intent here is to allow you to demonstrate what problem solving and programming skills you have mastered.

NOTICE

You used split to create an array in the previous assignment. You should know how to use split from doing that assignment. Look at your code to review if you need to.

DO THIS

1. Create javascript 14 array 05.js with a general function: paidInsuranceCost() with four parameters:

delimiter

delimitedListProductCosts

maxPriceForNoInsuranceCost

insuranceCostPercent

2. The function will perform the following:

Make an array of delimitedListProductCosts

Process all array values and compare each value in list to maxPriceForNoInsuranceCost

Compute the amount over

Multiply that amount by insuranceCostPercent.

Add result to total.

Return total after the loop finishes. Never return in the middle of a loop.

3. TIP

a. Use split to create an array of product costs.

b. Use a for loop to process each value in the array.

c. For each product cost more than maxPriceForNoInsuranceCost

compute the insurance cost for the portion of the product cost that is more than maxPriceForNoInsuranceCost

d. For each product cost <= maxPriceForNoInsuranceCost there is no insurance cost.

EXAMPLE

delimiter: $

delimitedListProductCosts: 200$345$100$500

maxPriceForNoInsuranceCost: 300

insuranceCostPercent: .1

In this example total would be:

(45 * .1) + (200 * .1) ==> 4.5 + 20 ==> 24.5

EXPLANATION

Product cost #1 = 200. It is <= 300, so there is no insurance cost.

Product cost #2 = 345. It is not <= 300, so compute the insurance cost as:

(345-300) * insuranceCostPercent

Product cost #3 = 100. It is <= 300, so there is no insurance cost.

Product cost #4 = 500. It is not lower than 300, so compute the insurance cost as:

(500-300) * insuranceCostPercent

HERE IS THE LOGIC TO USE

1. Your function receives four parameters in variables.

2. Create an array by splitting the data list (delimitedListProductCosts) using the delimiter. You did this in the previous assignment.

3. Use a for loop to process each value in the array.

4. For each value > maxPriceForNoInsuranceCost do this:

a. Calculate the amount maxPriceForNoInsuranceCost is less than the array value.

b. Multiply that result by the insuranceCostPercent.

c. Add that amount to the total.

5. After the loop return the total.

A. INSTRUCTIONS
          

ARRAYS AGAIN

OBJECTIVE

Practice again with arrays.

BACKGROUND

Your company sells products to customers. For each product the insurance is free up to a certain product cost.

Product cost above that maximum is the basis for insurance cost.

Calculate the total cost of insurance for a list of products sold.

NOTICE

These instructions are deliberately brief. The intent here is to allow you to demonstrate what problem solving and programming skills you have mastered.

NOTICE

You used split to create an array in the previous assignment. You should know how to use split from doing that assignment. Look at your code to review if you need to.

DO THIS

1. Create javascript 14 array 05.js with a general function: paidInsuranceCost() with four parameters:

delimiter

delimitedListProductCosts

maxPriceForNoInsuranceCost

insuranceCostPercent

2. The function will perform the following:

Make an array of delimitedListProductCosts

Process all array values and compare each value in list to maxPriceForNoInsuranceCost

If an array value is more than maxPriceForNoInsuranceCost

Compute the amount over

Multiply that amount by insuranceCostPercent.

Add result to total.

Return total after the loop finishes. Never return in the middle of a loop.

3. TIP

a. Use split to create an array of product costs.

b. Use a for loop to process each value in the array.

c. For each product cost more than maxPriceForNoInsuranceCost

compute the insurance cost for the portion of the product cost that is more than maxPriceForNoInsuranceCost

d. For each product cost <= maxPriceForNoInsuranceCost there is no insurance cost.

EXAMPLE

delimiter: $

delimitedListProductCosts: 200$345$100$500

maxPriceForNoInsuranceCost: 300

insuranceCostPercent: .1

In this example total would be:

(45 * .1) + (200 * .1) ==> 4.5 + 20 ==> 24.5

EXPLANATION

Product cost #1 = 200. It is <= 300, so there is no insurance cost.

Product cost #2 = 345. It is not <= 300, so compute the insurance cost as:

(345-300) * insuranceCostPercent

Product cost #3 = 100. It is <= 300, so there is no insurance cost.

Product cost #4 = 500. It is not lower than 300, so compute the insurance cost as:

(500-300) * insuranceCostPercent

HERE IS THE LOGIC TO USE

1. Your function receives four parameters in variables.

2. Create an array by splitting the data list (delimitedListProductCosts) using the delimiter. You did this in the previous assignment.

3. Use a for loop to process each value in the array.

4. For each value > maxPriceForNoInsuranceCost do this:

a. Calculate the amount maxPriceForNoInsuranceCost is less than the array value.

b. Multiply that result by the insuranceCostPercent.

c. Add that amount to the total.

5. After the loop return the total.

Explanation / Answer

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" >

function myFunction() {

var lDelimiter=document.getElementById("delimiter").value;

var lProductCostsArray = document.getElementById("delimitedListProductCosts").value.split(lDelimiter);

var lMaxPriceForNoInsuranceCost= document.getElementById("maxPriceForNoInsuranceCost").value;

var lInsuranceCostPercent=document.getElementById("insuranceCostPercent").value;

var arrayLength = lProductCostsArray.length;

var result=0;

//iterating product cost and applying our business logic

for (var i = 0; i < arrayLength; i++) {

if(lProductCostsArray[i]>=lMaxPriceForNoInsuranceCost)

{

result+=(lProductCostsArray[i]-lMaxPriceForNoInsuranceCost)*(lInsuranceCostPercent);

}

}

document.getElementById("result").innerHTML=result;

}

</script>

<style>

label { width: 200px; float: left; margin: 0 20px 0 0; }

span { display: block; margin: 0 0 3px; font-size: 1em; font-weight: bold; }

input { width: 200px; border: 1px solid #000; padding: 5px; }

</style>

</head>

<body>

<form>

<label for="EnterValue">

<span>Enter Values</span>

</label>

<br>

<label for="delimiter">

<span>Delimiter</span>

<input type="text" id="delimiter" />

</label>

<label for="delimitedListProductCosts">

<span>DelimitedListProductCosts</span>

<input type="text" id="delimitedListProductCosts" />

</label>

<label for="maxPriceForNoInsuranceCost">

<span>MaxPriceForNoInsuranceCost</span>

<input type="text" id="maxPriceForNoInsuranceCost" />

</label>

<label for="insuranceCostPercent">

<span>InsuranceCostPercent</span>

<input type="text" id="insuranceCostPercent" />

</label>

<button type="button" id="insurenceBtn">Calculate</button>

<br><br>

<label for="result">

<span id="result">Result Calculating...</span>

</label>

</form>

</body>

</html>