0

I have to do this simple exercise but I am really struggling a lot with it. I have to convert all the names to uppercase and pass it to the empty array, using only while loop(s), and I can't use any method, just plain javascript 5:

var names = ['rob', 'dwayne', 'james', 'larry', 'steve'];

var UpperCase = [];

I tried with an object like this (there are still some mistakes, it's just an idea):

var change = {
  "mary":"MARY",
  "john":"JOHN",
  "james":"JAMES",
  "anna":"ANNA",
  "jack":"JACK",
  "jeremy":"JEREMY",
  "susanne":"SUSANNE",
  "caroline":"CAROLINE",
  "heidi":"HEIDI"
}
for(var key in change) {
  var obj = change[key];

  for (var prop in obj) {
  var value = obj[prop];
}
}

while(names[i]===change.key) {
  for(var i=0; i<names.length; i++) {
  UpperCase = change.value[i];
} }

I hope somebody can help me with this one.

Thanks!

Pepdbm 7
  • 189
  • 3
  • 9
  • 2
    `I can't use any method`...like?????????? – Mamun May 24 '18 at 12:17
  • 1
    `toUpperCase` is plain javascript – Marcos Casagrande May 24 '18 at 12:20
  • It's basically impossible to answer this because you haven't clearly conveyed what are the limitations of this exercise. I assume the instructions are written down somewhere. Can you share them with us instead of giving us a watered-down version of them? – JLRishe May 24 '18 at 12:20
  • https://www.w3schools.com/jsref/jsref_obj_string.asp Here is a link that will open up some new worlds for you – TheCrzyMan May 24 '18 at 12:29

4 Answers4

0

You need to create an increment (i) so your while loop knows when to stop. It's a better case for a for loop but a while loop works as well.

var names = ['rob', 'dwayne', 'james', 'larry', 'steve']
var i = 0
var result = []

while (i < names.length) {
  result.push(names[i].toUpperCase())
  i++ 
}

console.log(result)
andrewgi
  • 620
  • 1
  • 7
  • 22
0

you should use handler function in map:

var names = ['rob', 'dwayne', 'james', 'larry', 'steve'];
var UpperCase = names.map(function(x){ return x.toUpperCase() });
Nasim
  • 325
  • 2
  • 14
  • Including a link because I don't know if OP knows very much about `map` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map – TheCrzyMan May 24 '18 at 12:33
  • The exercise is supposed to be avoiding using any helper or function, just the simplest javascript possible you can, using while. thanks – Pepdbm 7 May 25 '18 at 10:08
0

Instead of using toUppercase():

Check if a character is uppercase or lowercase with this

str[i].charCodeAt(0) > 96

charCodeAt

fromCharCode

Using just while

var i = 0;
while (i < str.length) ...

Not using push function for array

 UPPERCASE[UPPERCASE.length] = uppercase(names[i]);

I hope this function helps you

function uppercase(str) {
  var i = 0;
  var output = "";
  while (i < str.length) {
    var x =
      (str[i].charCodeAt(0) > 96)
        ? String.fromCharCode(str[i].charCodeAt(0) - 32)
        : String.fromCharCode(str[i].charCodeAt(0));
    output += x;
    i++;
  }
  return output;
}

Example:

function uppercase(str) {
  var i = 0;
  var output = "";
  while (i < str.length) {
    var x =
      (str[i].charCodeAt(0) > 96) ?
      String.fromCharCode(str[i].charCodeAt(0) - 32) :
      String.fromCharCode(str[i].charCodeAt(0));
    output += x;
    i++;
  }
  return output;
}
var UPPERCASE = [];
var names = ["rOb", "dwayne", "james", "larry", "steve"];

var i = 0;

while (i < names.length) {
  UPPERCASE[UPPERCASE.length] = uppercase(names[i]);
  i++;
}

console.log(UPPERCASE);

Update

Answer without function and linear condition

var UPPERCASE = [];
var names = ["rOb", "dwayne", "james", "larry", "steve"];

var i = 0;

while (i < names.length) {
  var j = 0,
    output = "",
    str = names[i];
  while (j < str.length) {
    var x;
    if (str[j].charCodeAt(0) > 96) {
      x = String.fromCharCode(str[j].charCodeAt(0) - 32);
    } else {
      x = String.fromCharCode(str[j].charCodeAt(0));
    }
    output += x;
    j++;
  }
  UPPERCASE[UPPERCASE.length] = output;
  i++;
}

console.log(UPPERCASE);
Saeed
  • 5,413
  • 3
  • 26
  • 40
  • That was a good one mate! could you be able to do it maybe without creating a function? As for the "?" and the ":" are they like an if statement? I didn't know them yet – Pepdbm 7 May 25 '18 at 10:58
-1

Improving a bit on @andrewgi's answer:

var names = ['rob', 'dwayne', 'james', 'larry', 'steve']
var i = 0
var result = []

while (i < names.length) {
  result.push(names[i].toUpperCase())
  i++ 
}

console.log(result)

I removed the +1 after the names.length check and spelled toUpperCase with correct camel-case.

Rob Moll
  • 3,345
  • 2
  • 9
  • 15