Get help on our basic JavaScript and Algorithms Challenges. If you are posting code that is large use Gist - https://gist.github.com/ paste the link here.
function diffArray(arr1, arr2) {
var conc = arr1.concat(arr2);
var uniqueArray = conc.filter(function(item, pos) {
return conc.indexOf(item) !== pos;
});
var final = uniqueArray.filter(function(remove){
return remove === uniqueArray;
});
return final;
}
// conc = [1, "calf", 3, "piglet", 1, "calf", 3, 4]
//uniqueArray = [1, "calf", 3]
diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]);
heebiegeebee sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
:star2: 3074 | @chrono79 |http://www.freecodecamp.com/chrono79
var permArray = [];
if (str.length === 1 ) {
permArray.push(str);
}
im also certain this is a good way to start it, lol
[1, "calf", 3]
var final = conc.filter(function(remove){
return uniqueArray.indexOf(remove)===-1;
});
11 % 3 = remainder
=
is the assignment operator. Your variable should always be on the left
11 % 3 var = remainder
var remainder = .....
function fearNotLetter(str) {
var alphabetArray = ("abcdefghijklmnopqrstuvwxyz").split("");
var lowerCase = str.toLowerCase();
var first = alphabetArray.indexOf(lowerCase[0]);
var mismatch = '';
console.log(first);
for (var i=0; i<lowerCase.length; i++){
if (lowerCase[i] === alphabetArray[i]){
return alphabetArray[i];
} else {
mismatch = first + lowerCase[i];
}
}
return mismatch;
}
fearNotLetter("abce");
i
can be used in alphabetArray
zaluth sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
:star2: 3075 | @chrono79 |http://www.freecodecamp.com/chrono79
var remainder = 11 % 3
alphabetArray[i]
, because he thinks i
is only applicable or in scope of lowerCase
or something like that
i
is our iterating variable. It is incrementing each loop, starting at 0 until its < lowerCase.length
. It can be used to access the index of any variable
index = Math.floor((Math.random() * quotes.length) + 1) - 1;
i
should not be used in anything other than lowerCase
as it is somehow hurting the readability
lowerCase[i] === alphabetArray[i]
is true
and the it returns a
, we should probably instead test inequality then the return will work how we want.
if (lowerCase[i] !== alphabetArray[i]) { return alphabetArray[i]; }
i
rabowlen sends brownie points to @gregatgit :sparkles: :thumbsup: :sparkles:
:cookie: 817 | @gregatgit |http://www.freecodecamp.com/gregatgit
fetch
if it's a latest browser
function fearNotLetter(str) {
var alphabetArray = ("abcdefghijklmnopqrstuvwxyz").split("");
var lowerCase = str.toLowerCase();
var first = alphabetArray.indexOf(lowerCase[0]);
var mismatch = '';
console.log(first);
for (var i=0; i<lowerCase.length; i++){
if (lowerCase[i] !== alphabetArray[i]){
return alphabetArray[i];
}
}
}
jluboff sends brownie points to @iashraful :sparkles: :thumbsup: :sparkles:
:cookie: 556 | @iashraful |http://www.freecodecamp.com/iashraful
first
and mismatch
function fearNotLetter(str) {
var alphabetArray = ("abcdefghijklmnopqrstuvwxyz").split("");
var lowerCase = str.toLowerCase();
var missingChar;
for (var i=0; i<lowerCase.length; i++){
if (lowerCase[i] !== alphabetArray[i]){
missingChar = alphabetArray[i];
break;
}
}
return missingChar;
}
a
case
first
to handle cases where the test case is not starting from beginning, like defgi
first
variable we added back in stoneage, you need it to pass the last two tests
evan17gr sends brownie points to @forkerino :sparkles: :thumbsup: :sparkles:
:cookie: 543 | @forkerino |http://www.freecodecamp.com/forkerino
function fearNotLetter(str) {
var alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('');
var startFrom = alphabets.indexOf(str[0]);
var missingChar;
for(var i = 0, len = str.length; i < len; ++i) {
if (str[i] !== alphabets[startFrom+i]) {
missingChar = alphabets[startFrom+i];
break;
}
}
return missingChar;
}
fearNotLetter("abce");
&lang=en
in your url.
var url="http://api.forismatic.com/api/jsonp/";
???
=
, was a typo
function fearNotLetter(str) {
var curr;
var prev;
var missing;
for(var i = 1, len = str.length; i < len; ++i) {
curr = str.charCodeAt(i);
prev = str.charCodeAt(i-1);
if (curr > prev+1) {
missing = String.fromCharCode(prev+1);
break;
}
}
return missing;
}
fearNotLetter("abce");
size
, and you push it in a subarray of the result instead of the array itself.
function fearNotLetter(str) {
var start = str.charCodeAt(0);
for (var i = 1; i<str.length; i++){
if (str.charCodeAt(i) !== start+i) {
return String.fromCharCode(start+i);
}
}
return undefined;
}
size
) of the arr
to the result
and do that as many times as necessary.
function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for (var a = 0;a < multiplyAll.length;a++){
for (var b = 0;b < multiplyAll[a].length;b++){
product *= multiplyAll([a][b]);
}
}
// Only change code above this line
return product;
}
// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);
multiplyAll
is the name of the function
arr
which is the array.
waelazar sends brownie points to @heebiegeebee and @manish-giri :sparkles: :thumbsup: :sparkles:
:star2: 3599 | @manish-giri |http://www.freecodecamp.com/manish-giri
:cookie: 459 | @heebiegeebee |http://www.freecodecamp.com/heebiegeebee
product *= multiplyAll([a][b]);
i doubt this will work too
function sumAll(arr) {
arr = arr.sort(); // sorts array from lowest to highest
var min = arr[0]; // sets variable to the lowest value of array
var max = arr[1]; // sets variable to the highest value of array
var orig = min; // variable for keeping track of initial value of min
var result = 0; // variable to store results
while (min <= max) { //condition
min += min + 1 ; // min is icremented by itself plus 1
result = min; // result is given the value of min
min++; // min is incremented
}
return result + orig; // returns the value of the result plus the initial value of min
}
sumAll([10,5]);
// Setup
var myStr = "Jello World";
// Only change code below this line
myStr[0] = "H"; // Fix Me
myStr = "Hello World";
function sumAll(arr) {
arr = arr.sort(); // sorts array from lowest to highest
var min = arr[0]; // sets variable to the lowest value of array
var max = arr[1]; // sets variable to the highest value of array
var orig = min; // variable for keeping track of initial value of min
var result = 0; // variable to store results
while (min <= max) { //condition
min += min + 1 ; // min is icremented by itself plus 1
result = min; // result is given the value of min
min++; // min is incremented
}
return result + orig; // returns the value of the result plus the initial value of min
}
sumAll([10,5]);
var lowest = Math.min(arr[0], arr[1]);
and finishing at the highest
fer408 sends brownie points to @gregatgit and @heebiegeebee :sparkles: :thumbsup: :sparkles:
:cookie: 818 | @gregatgit |http://www.freecodecamp.com/gregatgit
:cookie: 460 | @heebiegeebee |http://www.freecodecamp.com/heebiegeebee
function titleCase(str) {
for(var i=0;i<str.length;i++)
if(str[i]==' ')
{
str[i]='g';
}
return str;
}
@HeebieGeeBee like this?
// Setup var myStr = "Hello Jorld";
// Only change code below this line
var myStr = "Hello Jorld";
myStr = "Hello World";
myStr[0] = "H"; // Fix Me
myStr[0] = "H";
this line must be removed as it is a type error - trying to change single character in string
function fearNotLetter(str) {
var arr = str.split('');
var charCode = 0;
for (x=0;x<(arr.length - 1);x++){
charCode = (str.charCodeAt(x) + 1);
if (str.charCodeAt(x + 1) != charCode){
arr.splice((x + 1), 0, (String.fromCharCode(charCode)));
}
}
str = arr.join('');
return str;
}
fearNotLetter("abce");
Console says "potential infinite loop at line 5". Any suggestions? (line 5 is the "charCode = (str.char....... line)
function sumPrimes(num) {
var numbers = [];
var primes = [];
for (i = 2; i <= num; i++ ){
numbers.push(i);
}
primes = numbers;
for (i = 0; i < numbers.length; i++){
for (j = 0; j < numbers.length; j++){
if (numbers[j+1]%numbers[i] === 0){
primes.splice(j+1,j+2);
}
}
}
num = primes.reduce(function(prevVal, currVal, index, array) { return prevVal + currVal;});
return num;
}
sumPrimes(10);
My problem is why my output is always 2. Both arrays are reduced to one element. Even "numbers" array, despite I didn't operate on that array with splice. What am I missing? It should efficiently delete from "primes" all nonprime numbers.
function sumPrimes(num) {
var numbers = [];
var primes = [];
for (i = 2; i <= num; i++ ){
numbers.push(i);
}
primes = numbers;
for (i = 0; i < numbers.length; i++){
for (j = 0; j < numbers.length; j++){
if (numbers[j+1]%numbers[i] === 0){
primes.splice(j+1,j+2);
}
}
}
num = primes.reduce(function(prevVal, currVal, index, array) { return prevVal + currVal;});
return num;
}
sumPrimes(10);
My problem is why my output is always 2. Both arrays are reduced to one element. Even "numbers" array, despite I didn't operate on that array with splice. What am I missing? It should efficiently delete from "primes" all nonprime numbers.
brycemcdonald86 sends brownie points to @mitron6 :sparkles: :thumbsup: :sparkles:
:cookie: 433 | @mitron6 |http://www.freecodecamp.com/mitron6
nothing changes. Now when i fixed splice arguments it returns me an array numbers [2,5,9] And dont exactly understand why. I expected [2,3,5,7]. Not long ago I made simple program in Java to search for primes with that algorithm.
Its 3:00am here. Maybe i've got to get some sleep and try in couple of hours. I dunno
function sumPrimes(num) {
var numbers = [];
for (i = 2; i <= num; i++ ){
numbers.push(i);
}
for (i = 0; i < numbers.length; i++){
for (j = 1; j < numbers.length; j++){
if (numbers[j]%numbers[i] === 0){
numbers.splice(j,1);
}
}
}
num = numbers.reduce(function(prevVal, currVal, index, array) { return prevVal + currVal;});
return numbers;
}
sumPrimes(10);
function chunkArrayInGroups(arr, size) {
var newArr = [];
var arr1 = [0, 0];
for (var i = 0 ; i < arr.length ; size++) {
arr1 = arr.slice(i, size);
newArr.push(arr1);
return newArr;
}
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
lotosiauke sends brownie points to @heebiegeebee and @sjames1958gm :sparkles: :thumbsup: :sparkles:
:cookie: 461 | @heebiegeebee |http://www.freecodecamp.com/heebiegeebee
:star2: 5199 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
function palindrome(str) {
str.replace(/[^a-z|1-9]/g, "");
if (str.toLowerCase() === str.split('').reverse().join('').toLowerCase()){
return true;
}else{// Good luck!
return false;
}
}
palindrome("tsdsdf");
str.replace(/[^a-z|1-9]/g, "");
a new string is returned and you're didn't store it
str.replace(/[^a-z|1-9]/g, "");
you have replaced but the new string is not used str
like you did is just the str from the parameter
str
in the comparison you did like @sjames1958gm illustrated
function palindrome(str) {
var newStr = str.replace(/[^a-z|1-9]/g, "");
if (newStr.toLowerCase() === str.split('').reverse().join('').toLowerCase()){
return true;
}else{// Good luck!
return false;
}
}
palindrome("tsdsdf");
/[^a-zA-Z0-9]/g
use this for your regez
str.split('').reverse().join('').toLowerCase()
use newStr
not str
unlawfulspoonisback sends brownie points to @lumexralph and @heebiegeebee and @sjames1958gm :sparkles: :thumbsup: :sparkles:
:cookie: 675 | @lumexralph |http://www.freecodecamp.com/lumexralph
:cookie: 462 | @heebiegeebee |http://www.freecodecamp.com/heebiegeebee
:star2: 5200 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
// Setup
var myStorage = {
"car": {
"inside": {
"glove box": "maps",
"passenger seat": "crumbs"
},
"outside": {
"trunk": "jack"
}
}
};
// Only change code below this line
var gloveBoxContents = "maps";
myStorage.car.inside["glove box"];// Change this line
myStorage.car.inside["glove box"]
not maps
Array.prototype.slice(arguments);
\\
that's backslash not \b
\\
before and after SecondLine
like this @Lumexralph
// Setup
var myStorage = {
"car": {
"inside": {
"glove box": "maps",
"passenger seat": "crumbs"
},
"outside": {
"trunk": "jack"
}
}
};
// Only change code below this line
var gloveBoxContents = "myStorage.car.inside[glove box]"; // Change this line
"glove box"
astewart400 sends brownie points to @lumexralph :sparkles: :thumbsup: :sparkles:
:cookie: 676 | @lumexralph |http://www.freecodecamp.com/lumexralph
tech-tron sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 5201 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
vzambrano98 sends brownie points to @khaduch :sparkles: :thumbsup: :sparkles:
:star2: 2224 | @khaduch |http://www.freecodecamp.com/khaduch
@Lumexralph
//Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUpProfile(firstName, prop){
// Only change code below this line
if (contacts.hasOwnProperty(firstName) && contacts.hasOwnProperty(prop)){
if (firstName===undefined){
return "No such contact";
} else if (prop===undefined){
return "No such property";
}
return contacts[prop];
}
);
//second for loop
//first for loop
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
instructions:
We have an array of objects representing different people in our contacts lists.
A lookUpProfile function that takes firstName and a property (prop) as arguments has been pre-written for you.
The function should check if firstName is an actual contact's firstName and the given property (prop) is a property of that contact.
If both are true, then return the "value" of that property.
If firstName does not correspond to any contacts then return "No such contact"
If prop does not correspond to any valid properties then return "No such property"
.hasOwnProperty
Q? Write a function nextInLine which takes an array (arr) and a number (item) as arguments. Add the number to the end of the array, then remove the first element of array. The nextInLine function should then return the element that was removed.
function nextInLine(arr, item) {
// Your code here
return item; // Change this line
}
I don't understand the wording of this question whatsoever. anyone have a link to read up on and or wanna help? Pls
@Lumexralph so I think I'm over thinking the loop. If the loop is as follows:
for (i=0; i<contacts.length; i++){}
won't it just keep going through? What will make it stop at the appropriate firstName or prop?
and I will definitely re check myself @khaduch
item
into arr
and return the the first element in arr
.shift
, .unshift
, .push
and .pop
(I think I have those right...) Along with the description "Add the number to the end of the array, then remove the first element of array. " you have to figure out which Array methods do those things. Then try it - post code and ask more questions!
i<contacts.length
stops the loop to when the condition is false
@vzambrano98 - look for the secret word:
If both are true, then return
the "value" of that property.
If firstName does not correspond to any contacts then return
"No such contact"
If prop does not correspond to any valid properties then return
"No such property"
:)
vzambrano98 sends brownie points to @khaduch and @lumexralph :sparkles: :thumbsup: :sparkles:
:cookie: 677 | @lumexralph |http://www.freecodecamp.com/lumexralph
:warning: vzambrano98 already gave khaduch points
function randomArticle(){
location.href = 'https://en.wikipedia.org/wiki/Special:Random';
}
function getSearchTerm(){
var term = $("#searchTerm").val();
return term;
}
function getJson(term){
var jsonURL = 'https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=' + term;
return jsonURL;
}
function processJSON(jsonURL){
var data = $.getJSON(jsonURL, function(data){
getTitles(data);
});
}
function getTitles(json){
var titles = [];
if (json && json.query && json.query.pages){
var pages = json.query.pages;
} else {
alert("Unable to process json");
return;
}
for(var id in pages){
var title = pages[id].title; // We will assign the current title to the variable title
if(title){
titles.push(title); // Push the current title into the array of titles
} else {
alert('Unable retreive title');
return;
}
}
alert(titles);
return titles;
}
$(document).ready(function(){
$('#searchWiki').on('click', function(){
var searchTerm = getSearchTerm();
var jsonURL = getJson(searchTerm);
processJSON(jsonURL);
});
});
// Setup
var testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};
// Only change code below this line;
var playerNumber =["Montana"]; // Change this Line
var player = testObj[16]; // Change this Line
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
var result = "";
// Your code below this line
// Your code above this line
return result;
}
// Change the words here to test your function
wordBlanks("dog", "big", "ran", "quickly");
function processJSON(jsonURL){
var data = $.getJSON(jsonURL, function(data){ // request here is asynchronous and not synchronous
getTitles(data); //this function is called while the data is still being requested which will return data as undefined
});
data is toring whatever value getTitles returns, remove the `var data`
}`
@HeebieGeeBee
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb)
{
var result = "myNone, myAdjective, myVerb, myAdverb";
// Your code below this line
// Your code above this line
return result;
}
// Change the words here to test your function
wordBlanks("dog", "big", "ran", "quickly");
i < contacts.length
as your loop control variable will make the for
loop exit after looking at all of the contacts. If you get to this point, you haven't found a contact that matches the firstName
(hint... hint... )if
statement to check for the name match, and if one is found, you return one of the two return values that are possible when you match the firstName
. (Which will end the loop at that point.)
@HeebieGeeBee
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb)
{
var result = "myNone, myAdjective, myVerb, myAdverb";
// Your code below this line
// Your code above this line
return result;
}
// Change the words here to test your function
wordBlanks("dog", "big", "ran", "quickly");
why does
{foo: 'bar'} === x
yield true?
x
?
@HeebieGeeBee
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb)
{
var result = "myNone, myAdjective, myVerb, myAdverb";
// Your code below this line
result= myNone +"dog"+ myAdjective + "big" + myVerb +"ran"+ myAdverb+"quickly";
result= myNone +"dog"+ myAdjective + "big" + myVerb +"ran"+ myAdverb+"slowly";
// Your code above this line
return result;
}
// Change the words here to test your function
wordBlanks("dog", "big", "ran", "quickly");
myNone
should be myNoun
put space also in the string literals
@HeebieGeeBee
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb)
{
var result = "myNone, myAdjective, myVerb, myAdverb";
// Your code below this line
result= myNone+ "dog" +myAdjective+ "big" +myVerb+ "ran" +myAdverb+ "quickly";
result= myNone+ "cat" +myAdjective+ "little" +myVerb+ "hit" +myAdverb+ "slowly";
// Your code above this line
return result;
}
// Change the words here to test your function
wordBlanks("dog", "big", "ran", "quickly");
wordBlanks("cat", "little", "hit", "slowly");
function findLongestWord(str) {
var newStr = str.split(",");
var x = newStr[""];
var y = newStr[""];
if( x > y){
return newStr[x].length;
}
}
findLongestWord("The quick brown fox jumped over the lazy dog");
jainishpanchal sends brownie points to @heebiegeebee :sparkles: :thumbsup: :sparkles:
:cookie: 463 | @heebiegeebee |http://www.freecodecamp.com/heebiegeebee
unlawfulspoonisback sends brownie points to @lumexralph and @heebiegeebee :sparkles: :thumbsup: :sparkles:
:cookie: 464 | @heebiegeebee |http://www.freecodecamp.com/heebiegeebee
:cookie: 678 | @lumexralph |http://www.freecodecamp.com/lumexralph
var countdown = function(value) {
if (value > 0) {
console.log(value);
return countdown(value - 1);
} else {
return value;
}
};
countdown(10);
how is this looping?
sirus3020 sends brownie points to @heebiegeebee and @lumexralph :sparkles: :thumbsup: :sparkles:
:cookie: 465 | @heebiegeebee |http://www.freecodecamp.com/heebiegeebee
:cookie: 679 | @lumexralph |http://www.freecodecamp.com/lumexralph
:bulb: to format code use backticks! ``` more info
var singleVal = array.reduce(function(previousVal, currentVal){
return previousVal - currentVal;
});
Use the reduce method to sum all the values in array and assign it to singleVal.
Need help
+
not -
//Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUpProfile(firstName, prop){
// Only change code below this line
for (var i=0; i<contacts.length; i++){
for (var j=0; j<contacts[i].length;j++){
if (contacts[i][j][prop]===undefined){
return "No such property";
} else if (contacts[i][j][firstName]===undefined){
return "No such contact";
} else if (contacts[i][j].hasOwnProperty(firstName) && contacts[i].hasOwnProperty(prop)){
return contacts[i][j][prop];
}}
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
function findLongestWord(str) {
var words = str.split(" ");
var max = 0;
for (i = 0; i < max.length; i++) {
if (words[i].length > max.length) {
return words[i].lenghth;
}
}
}
findLongestWord("The quick brown fox jumped over the lazy dog");
:bulb: to format code use backticks! ``` more info
function findLongestWord(str) {
var words = str.split(" ");
var max = 0;
for (i = 0; i < max.length; i++) {
if (words[i].length > max.length) {
max.length = words[i].lenghth;
}
}
return words[i].length;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
function findLongestWord(str) {
var words = str.split(" ");
var max = 0;
for (i = 0; i < max; i++) {
if (words[i].length > max) {
max = words[i].length;
}
}
return words[i].length;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
function uniteUnique(arr) {
var args = Array.prototype.slice.call(arguments);
console.log(args);
args.reduce(function(){
});
return arr;
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
@vzambrano98 - sorry, I spaced out for a while... getting late! :)
Let's see - you do not need a second for
loop- this is an array of objects, so you need the loop with the contacts.length
to iterate through the array. The objects, which are accessed with contacts[i]
as the reference to the object, can be directly accessed with the properties in one form or another. It looks like you have the basic idea, but that inner loop is doing something that is not useful. If it was an array of arrays, then that would be a different story.
So you're on the right track - contacts[i].firstName
can be used directly to access the firstName
property of each contact object.
To access the other values (other properties) you will be given property names in the variable (function argument) prop
- and you can use contacts[i][prop]
to access those, contacts[i].hasOwnProperty(prop)
to check that the property exists on a given object.
Why don't you digest that and try to make some changes. I'm going to be on for a short while, see if you get closer... (BTW, there is still a problem with the conditions under which you are returning "No such contact"
var obj={
name:"Holly",
age:35,
role:"producer"
}
function convertObjectToList(obj) {
return ;
}
:bulb: to format code use backticks! ``` more info
[ ["name", "Holly"], ["age", 35}
etc.?
max
function findLongestWord(str) {
var words = str.split(" ");
var max = 0;
for (i = 0; i < max; i++) {
if (words[i].length > max) {
max = words[i].length;
}
}
return words[i].length;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
Object.keys
(if I remember correctly) that will give you an array of the object keys. Then you could just iterate over that array, and generate the sub arrays. Does that give you something to go on?
return words[i].length;
change to return max;
function findLongestWord(str) {
var words = str.split(" ");
var max = 0;
for (i = 0; i < max; i++) {
if (words[i].length > max) {
max = words[i].length;
}
}
return words[i].length;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
function findLongestWord(str) {
var words = str.split(" ");
var max = 0;
for (i = 0; i < max; i++) {
if (words[i].length > max) {
max = words[i].length;
}
}
return max;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
:trollface: troll problems? notify admins here
@Lumexralph
var count = 0;
var max = str.split(' ');
for (var i = 0; i < max.length; i++) {
if ( max[i].length > count ) {
count = max[i].length;
}
}
is the same as (well same results)
str = str.split(' ');
for (var i = 0; i < str.length; i++) {
if ( str[i].length > count ) {
count = str[i].length;
}
}
function findLongestWord(str) {
var words = str.split(' ');
var max = 0;
for (var i = 0; i < words.length; i++) {
if (words[i].length > max) {
max = words[i].length;
}
}
return max;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
unlawfulspoonisback sends brownie points to @lumexralph and @heebiegeebee :sparkles: :thumbsup: :sparkles:
:warning: unlawfulspoonisback already gave heebiegeebee points
:warning: unlawfulspoonisback already gave lumexralph points
function lookUpProfile(firstName, prop){
// Only change code below this line
var i = 0;
while (i < contacts.length) {
if (contacts[i].firstName == firstName) {
if(contacts[i].hasOwnProperty(prop) === true) {
return contacts[i][prop];
} else {
return "No such property";
}
} else {
if (i < contacts.length) {
i++;
} else {
return "No such contact";
}
}
}
// Only change code above this line
}
" "
between each word, and return that "sentence" to the caller. Fairly straightforward (although this seems to be a challenge that stumps a lot of people.)
i
past the length, the while loop will terminate. You should put your return "No such contact";
in a place after the loop ends, because then you will know that you found no match.
myNoun + " " + myVerb
, etc.
jl-code1721 sends brownie points to @khaduch :sparkles: :thumbsup: :sparkles:
:star2: 2225 | @khaduch |http://www.freecodecamp.com/khaduch
for
loop - while you are within the list. After you finish the list, your loop will end, and if you haven't returned from within the loop, return after the body of the loop.
ERROR :worried: nextInLine([], 1) should return 1
function nextInLine(arr, item) {
// Your code here
return arr.shift(); // Change this line
}
// Test Setup
var testArr = [1,2,3,4,5];
// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
6thsen sends brownie points to @khaduch and @lumexralph :sparkles: :thumbsup: :sparkles:
:cookie: 680 | @lumexralph |http://www.freecodecamp.com/lumexralph
:star2: 2226 | @khaduch |http://www.freecodecamp.com/khaduch
Guys I'm completely stuck on this one.
Assignment:In Computer Science a queue is an abstract Data Structure where items are kept in order. New items can be added at the back of the queue and old items are taken off from the front of the queue.
Write a function nextInLine which takes an array (arr) and a number (item) as arguments. Add the number to the end of the array, then remove the first element of array. The nextInLine function should then return the element that was removed.
Code:
// Example
var ourArray = ["Stimpson", "J", ["cat"]];
removedFromOurArray = ourArray.shift();
// removedFromOurArray now equals "Stimpson" and ourArray now equals ["J", ["cat"]].
// Setup
var myArray = [["John", 23], ["dog", 3]];
// Only change code below this line.
var removedFromMyArray;
:bulb: to format code use backticks! ``` more info
In Computer Science a queue is an abstract Data Structure where items are kept in order. New items can be added at the back of the queue and old items are taken off from the front of the queue.
Write a function nextInLine which takes an array (arr) and a number (item) as arguments. Add the number to the end of the array, then remove the first element of array. The nextInLine function should then return the element that was removed.
function nextInLine(arr, item) {
// Your code here
arr = testArr;
item = 5;
testArr.push(item);
testArr.shift();
return item; // Change this line
}
// Test Setup
var testArr = [1,2,3,4,5];
// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
arr
is the array not testArray
, use the parameter
yori1 sends brownie points to @lumexralph :sparkles: :thumbsup: :sparkles:
:cookie: 681 | @lumexralph |http://www.freecodecamp.com/lumexralph
anshuldawar sends brownie points to @lumexralph :sparkles: :thumbsup: :sparkles:
:cookie: 682 | @lumexralph |http://www.freecodecamp.com/lumexralph
tech-tron sends brownie points to @lumexralph :sparkles: :thumbsup: :sparkles:
:cookie: 683 | @lumexralph |http://www.freecodecamp.com/lumexralph
@khaduch haha thanks, I cant figure out the solution to that same code from before var obj={
name:"Holly",
age:35,
role:"producer"
}
function convertObjectToList(obj) {
return;
} cant figure out how to turn object literal into an array of arrays...
breese8009 sends brownie points to @khaduch :sparkles: :thumbsup: :sparkles:
:star2: 2227 | @khaduch |http://www.freecodecamp.com/khaduch
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
if(str.substr(-1)==target){
return true;}
else {return false;}
}
confirmEnding("He has to give me a new name", "me");
var obj = {
name: 'Holly',
age: 35,
role: 'producer'
}
function convertObjectToList(obj) {
var myArr = [
];
for (var i in obj) {
console.log(i);
console.log(obj[i]);
myArr.push([i,
obj[i]]);
}
return myArr;
}
var result = convertObjectToList(obj);
console.log(result)
FirstLinenewlinebackslashSecondLinebackslashcarriage-returnThirdLine
var myStr = "FirstLine\n\SecondLine\\rThirdline";
I keep getting an error for this. Can anyone help me or is this a bug?
function titleCase(str) {
var word = str.split("");
word.slice().charCodeAt(0).toUpperCase();
}
titleCase("I'm a little tea pot");
My code is not working for the challenge title case a sentence
target.length
in there.
\\
before and after SecondLine
function titleCase(str) {
var word = str.split("");
word.slice().charCodeAt(0).toUpperCase();
}
titleCase("I'm a little tea pot");
MY CODE IS NOT WORKING NEED HELP
function lookUp(firstName, prop){
// Only change code below this line
for(var i=0;i<contacts.length;i++){
if(contacts[i].firstName == firstName){
if(contacts[i].hasOwnProperty(prop)){
return contacts[i].prop;
}else{
return "No such property";
}
}
if(i == contacts.length-1){
return "No such contact";
}
}
prop
is a variable dot method won't work use [ ]
method
str
argument, and subtract the length of the target
argument, and that is what you pass to the .substring
method. It tells you where in the string (the index) to start extracting the substring, and if you only pass one argument, it will extract all the way to the end.
word
variable and do a toUpperCase() to the first letter
function titleCase(str) {
var word = str.split(" ");
for (var i = 0; i < word.length; i++) {
word[i].substring(0,1).toUpperCase();
}
}
function lookUp(firstName, prop){
// Only change code below this line
for(var i=0;i<contacts.length;i++){
if(contacts[i][firstName] === firstName){
if(contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}else{
return "No such property";
}
}
if(i == contacts.length-1){
return "No such contact";
}
}
if(contacts[i][firstName] === firstName){
should be if(contacts[i]["firstName"] === firstName){
function titleCase(str) {
var splitted = str.split(" ");
for (var i = 0; i < splitted.length; i++) {
splitted[i] = splitted[i].substring(0,1).toUpperCase() + splitted[i].substring(1).toLowerCase();
}
return splitted.join(" ");
}
titleCase("I'm a little tea pot");
Object.firstName or Object["firstName"]
they are both properties in object
firstName
property in contacts to check against firstName
parameter
function destroyer(arr) {
var removeOne = arr.arguments([0][1]);
var removeTwo = arr.arguments([0][2]);
arr2 = [];
for(var i = 1; i < arguments.length; i++) {
if(arr[i] === removeOne || arr[i] === removeTwo) {
arr2.push(arr[i]);
}
}
arr = arr2;
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
function switchOfStuff(val) {
var answer = "";
// Only change code below this line
switch(val){
case "a":
return "apple";
break;
case "b":
return "bird";
break;
case "c":
return "cat";
break;
default:
default "stuff";
}
// Only change code above this line
return answer;
}
// Change this value to test
switchOfStuff(1);
Adding a default option in Switch statements. Plz help me to solve this challenge
@HeebieGeeBee no the right function bellow function lookUp(firstName, prop){
// Only change code below this line
for(var i=0;i<contacts.length;i++){
if(contacts[i]["firstName"] == firstName){
if(contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}else{
return "No such property";
}
}
if(i == contacts.length-1){
return "No such contact";
}
}
// Only change code above this line
}
function destroyer(arr) {
var removeOne = arguments[0][1];
var removeTwo = arguments[0][2];
arr2 = [];
for(var i = 1; i < arguments[0][0].length; i++) {
if(arr[i] !== removeOne || arr[i] !== removeTwo) {
arr2.push(arr[i]);
}
}
arr = arr2;
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
default:
return "stuff";
answer
because you wouldn't be needing a break statement when using return in a switch statement
function destroyer(arr) {
console.log(arguments);
var removeOne = arguments[1];
var removeTwo = arguments[2];
arr2 = [];
for(var i = 1; i < arguments[0].length; i++) {
if(arr[i] !== removeOne || arr[i] !== removeTwo) {
arr2.push(arr[i]);
}
}
arr = arr2;
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
function switchOfStuff(val) {
var answer = "";
// Only change code below this line
switch (val) {
case "a":
answer = "apple";
break;
case "b":
answer ="bird";
break;
case "c":
answer = "cat";
break;
default:
default = "stuff";
}
// Only change code above this line
return answer;
}
// Change this value to test
switchOfStuff(1);
Adding a default option in Switch statements. Plz anyone help me to solve this challenge
@HeebieGeeBee
function nextInLine(arr, item) {
// Your code here
return item; // Change this line
}
// Test Setup
var testArr = [1,2,3,4,5];
// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
@ooade
function switchOfStuff(val) {
var answer = "";
// Only change code below this line
switch (val) {
case "a":
return"apple";
break;
case "b":
return"bird";
break;
case "c":
return "cat";
break;
default:
return "stuff";
}
// Only change code above this line
return answer;
}
// Change this value to test
switchOfStuff(1);
Like this?
Arguments[3]
0
:
Array[2]
1
:
"tree"
2
:
53
callee
:
destroyer(arr)
length
:
3
Symbol(Symbol.iterator)
:
values()
__proto__
:
Object
trupal24 sends brownie points to @ooade :sparkles: :thumbsup: :sparkles:
:cookie: 800 | @ooade |http://www.freecodecamp.com/ooade
function destroyer(arr) {
var removeOne = arguments[1];
var removeTwo = arguments[2];
arr2 = [];
for(var i = 0; i < arr.length; i++) {
if(arr[i] !== removeOne && arr[i] !== removeTwo) {
arr2.push(arr[i]);
}
}
arr = arr2;
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
var singleVal = array.reduce(function(previousVal, currentVal){
return previousVal+currentVal;
});
arr.push(item);
return arr.shift();
@jainishpanchal but think why this is the solution
function largestOfFour(arr) {
// You can do this!
var largest = [];
var larger = 0;
for (i=0; i < arr.length; i++){
for (j=0; j < arr[i].length; j++) {
if (arr[i][j] > larger) {
larger = arr[i][j];
largest[i] = arr[i][j];
}
}
}
return largest;
}
monroemann sends brownie points to @heebiegeebee and @ooade :sparkles: :thumbsup: :sparkles:
:cookie: 466 | @heebiegeebee |http://www.freecodecamp.com/heebiegeebee
:cookie: 801 | @ooade |http://www.freecodecamp.com/ooade
var singleVal = array.reduce(function(previousVal , currentVal){
return previousVal + currentVal;
});
need help
seanparkin sends brownie points to @heebiegeebee :sparkles: :thumbsup: :sparkles:
:cookie: 467 | @heebiegeebee |http://www.freecodecamp.com/heebiegeebee
0
?
var array = [4,5,6,7,8];
var singleVal = 0;
// Only change code below this line.
var singleVal = array.reduce(function(previousVal , currentVal){
return previousVal + currentVal;
});
singleVal = array;
singleVal = array;
and undeclare singleVal by removing the var keyword
hariharanguru83 sends brownie points to @ooade and @heebiegeebee :sparkles: :thumbsup: :sparkles:
:cookie: 468 | @heebiegeebee |http://www.freecodecamp.com/heebiegeebee
:cookie: 802 | @ooade |http://www.freecodecamp.com/ooade
<div class='class1 class2' id='id1'>Content here</div>
$('.class1 class2' '#id1').append('Content here')
function nextInLine(arr, item) {
// Your code here
nextInline.shift();
nextInLine.unshift(item);
function nextInLine(arr, item) {
// Your code here
nextInline.shift();
nextInLine.unshift(item);
function nextInLine(arr, item) {
// Your code here
item.push();
return shift; // Change
<Your code here>
s
a<your code here>
sa
without the "s" and "a" @kuehlc
<function cc(card) {
// Only change code below this line
if (++hand < 5) {
if (card > 1 || card < 7) {
count++;
} else if (card == 10 || card == 'J' || card == 'Q' || card == 'K' || card == 'A') {
count--;
}
} else {
count = 0;
hand = 1;
if (card > 1 || card < 7) {
count++;
} else if (card == 10 || card == 'J' || card == 'Q' || card == 'K' || card == 'A') {
count--;
}
}
if (count > 0) {
return count + " Bet";
} else {
return count + " Hold";
}
// Only change code above this line
}>
'<function cc(card) {
// Only change code below this line
if (++hand < 5) {
if (card > 1 || card < 7) {
count++;
} else if (card == 10 || card == 'J' || card == 'Q' || card == 'K' || card == 'A') {
count--;
}
} else {
count = 0;
hand = 1;
if (card > 1 || card < 7) {
count++;
} else if (card == 10 || card == 'J' || card == 'Q' || card == 'K' || card == 'A') {
count--;
}
}
if (count > 0) {
return count + " Bet";
} else {
return count + " Hold";
}
// Only change code above this line
}>'