0

Question: Develop an array of 1000 objects (having properties name and number as shown).

  1. We need a function to convert every object so the name is uppercased and values are 5 times the original and store into the higher variable. Similarly, another function that converts every object so the name is lower case and value is 3 times the original, store this into the little variable.
  2. We need a function that takes each object in higher and finds all objects in little that evenly divide into it. Example: 30 in higher object is evenly divided by 6 in little.
  3. The output of 2 must be an array of higher numbers, and in every object there should be got (which is a variable inside the object) which will contain every little object that evenly divided the higher.

My code:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script>
 var n = 1000;
var sample = [];
for (var i = 0; i < n; i++) sample.push({
 name:'John' + i,
 value: i 
});
console.log(sample);
function Converter() {
 var n = 1000;
 var higher = sample;
 for (var i = 0; i < n; i++) higher.name = 'John' + i;
 higher.value = i * 5;
 console.log(higher);
}
 </script>
</body>
</html>

The array of objects is created and it is as expected/required by the question, however, the converter function for higher does not work, also how should the 3rd question be done?

asked Oct 2, 2018 at 20:31
1
  • 2
    sample is the array, not an item inside the array. When you access higher.name and higher.value you are accessing an undefined property in the array. Also, your syntax is not correct, for has missing braces and you probably didn't intend to do that. Commented Oct 2, 2018 at 20:38

2 Answers 2

2

Some thoughts:

1) only constructors should start with a capital letter, functions should be camelcase by convention so it should be converter

2) you don't call converter() so it never gets executed

3) make sure to indent your code properly var n and var sample should be at the same depth.

4) if you omit the brackets after an if or for, only the following statement gets inside the branch, so in your case you do:

 for (var i = 0; i < n; i++)
 higher.name = 'John'+i;
 higher.value = i*5;

so the second line isn't even executed in the loop, you want:

for (var i = 0; i < n; i++) {
 higher.name = 'John'+i;
 higher.value = i*5;
}

5) higher.name makes little sense as higher is an array, you want to change the name of the ith higher number which you can do with higher[i].name

6) "John1" is not in caps, you want to call toUpperCase on it (("John1").toUpperCase())

also how should the 3rd question be done?

I guess fixing your code and doing the second question is enough for today.

You could continue reading:

Coding style matters

js array iterations

answered Oct 2, 2018 at 20:39
Sign up to request clarification or add additional context in comments.

4 Comments

How should I proceed with the 2nd question? I think I have to compare 2 arrays. This is one of the main questions that I'm stuck at.
@john first fix your code!! make yourself familiar with the console and the tools, get some more basics. You haven't even found the most basic errors in your code, you cannot write more conplex code without failing completely. Do it step by step.
Is it appropriate to suggest Array.prototype.map or will that be too much?
@slider if you look at john's question history, one can clearly see that he is dumping is homework here, full of typos & syntax errors and the community produces clean and copy+paste ready answers ... The array methods are definetly good to point out but I would not give any code.
1

You should also try to think in a more structured manner about your code here. I would suggest writing separate functions for each problem and giving them meaningful names. Perhaps something like the following:

var n = 1000;
var sample = [];
for (var i = 0; i < n; i++) sample.push({
 name: 'John' + i,
 value: i
});
console.log(sample);
var higher = convertToHigher(sample);
var little = convertToLittle(sample);
var higherWithDivisors = findAllDivisors(higher, little);
function convertToHigher(arr) {
 var newArr = [];
 // TODO: iterate through each entry in arr, create a new modified object 
 // with a higher value and add it to newArr
 return newArr;
}
function convertToLittle(arr) {
 var newArr = [];
 // TODO: iterate through each entry in arr, create a new modified object
 // with a lower value and add it to newArr
 return newArr;
}
function findAllDivisors(arr1, arr2) {
 var newArr = [];
 // TODO: solve problem 3 here
 return newArr;
}
answered Oct 2, 2018 at 21:07

1 Comment

Thank-you @slider. I tried to develop convertToHigher, however, it does not work. The code function convertToHigher(arr){ var newArr = []; var n = 1000; for (var i = 0; i < n; i++){ arr[i].name = 'JOHN' + i; arr.value = i * 5; } console.log(arr); return newArray; }

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.