Extract a string from and string of characters and change it case and loop through
Thu Aug 25 2022 22:53:27 GMT+0000 (Coordinated Universal Time)
Saved by
@Iron_chest
#javascript
'use strict';
let myString = 'I am really hungry for some';
console.log(myString);
// To transform myString in UPPERCASE
let myUpperString = myString.toUpperCase();
console.log(myUpperString);
// methods have ();
// To extract the word REALLY from the myString
// first, get the location
let exactLocation = myString.search('really');
console.log(exactLocation);
// To get the word in full
let extractWordInFull = myString.substr(exactLocation, 6);
console.log(extractWordInFull);
// turning the extracted word into uppercase
let turnUpperCase = extractWordInFull.toUpperCase();
console.log(turnUpperCase);
// replacing the word in the string
let newWord = myString.replace('really', turnUpperCase);
console.log(newWord);
let foods = ['bread', 'yam', 'rice', 'beans'];
let stringLiteral = `${myString} ${foods[0]}`;
console.log(stringLiteral);
// looping through a array together with a string
for(let i = 0; i < foods.length; i++) {
console.log(`${myString} ${foods[i]}`)
}
/* for(let eachFood of foods) {
console.log(`${myString} ${eachFood}`);
} */
for(let eachFoods of foods) {
console.log(`${myString} ${eachFoods}`);
}
// to convert to an uppercase if the index of the food is an even number
for ( let i = 0; i < foods.length; i++) {
if ( i % 2 === 0) {
let changeToUpperCase = foods[i].toUpperCase();
console.log(`${myString} ${changeToUpperCase}`);
}
else {
console.log(`${myString} ${foods[i]}`);
}
}
// I CAN ALWAYS REFACTOR MY CODE
content_copyCOPY
Comments